itertools

All possible ways to interleave two strings

守給你的承諾、 提交于 2019-11-30 17:11:56
I am trying to generate all possible ways to interleave any two arbitrary strings in Python. For example: If the two strings are 'ab' and 'cd' , the output I wish to get is: ['abcd', 'acbd', 'acdb', 'cabd', 'cadb', 'cdab'] See a is always before b (and c before d ). I am struggling to find a solution to this. I have tried itertools as shown below: import itertools def shuffle(s,t): string = s+t for i in itertools.permutations(string): print(''.join(i)) shuffle('ab','cd') But as expected, this returns all possible permutations disregarding order of a and b (and c and d ). The Idea Let the two

Equivalent Nested Loop Structure with Itertools

[亡魂溺海] 提交于 2019-11-30 12:12:43
Python's succint syntax through its batteries allows verbose code line to be expressed in readable one liners. Consider the following examples ====================================================| for a in range(3): | for b in range(3): | for c in range(3): | print (a,b,c), | - - - - - - - - - - - - - - - - - -| for e in product(range(3), repeat=3): | print e, | ====================================================| for a in range(3): | for b in range(a , 3): | for c in range(b , 3): | print (a,b,c), | - - - - - - - - - - - - - - - - - -| for e in combinations_with_replacement(range(3), 3):|

How to get all mappings between two lists?

▼魔方 西西 提交于 2019-11-30 11:07:18
We have two lists, A and B: A = ['a','b','c'] B = [1, 2] Is there a pythonic way to build the set of all maps between A and B containing 2^n (here 2^3=8)? That is: [(a,1), (b,1), (c,1)] [(a,1), (b,1), (c,2)] [(a,1), (b,2), (c,1)] [(a,1), (b,2), (c,2)] [(a,2), (b,1), (c,1)] [(a,2), (b,1), (c,2)] [(a,2), (b,2), (c,1)] [(a,2), (b,2), (c,2)] Using itertools.product , it's possible to get all the tuples: import itertools as it P = it.product(A, B) [p for p in P] Which gives: Out[3]: [('a', 1), ('a', 2), ('b', 1), ('b', 2), ('c', 1), ('c', 2)] thefourtheye You can do this with itertools.product and

Why does Python's itertools.cycle need to create a copy of the iterable?

为君一笑 提交于 2019-11-30 09:00:04
问题 The documentation for Python's itertools.cycle() gives a pseudo-code implementation as: def cycle(iterable): # cycle('ABCD') --> A B C D A B C D A B C D ... saved = [] for element in iterable: yield element saved.append(element) while saved: for element in saved: yield element Below, it states: "Note, this member of the toolkit may require significant auxiliary storage (depending on the length of the iterable)." I basically was going down this path, except I did this, which does not require

Find the index of a given combination (of natural numbers) among those returned by `itertools` Python module

社会主义新天地 提交于 2019-11-30 04:43:38
问题 Given a combination of k of the first n natural numbers, for some reason I need to find the position of such combination among those returned by itertools.combination(range(1,n),k) (the reason is that this way I can use a list instead of a dict to access values associated to each combination, knowing the combination). Since itertools yields its combinations in a regular pattern it is possible to do it (and I also found a neat algorithm), but I'm looking for an even faster/natural way which I

what is the quickest way to iterate through a numpy array

女生的网名这么多〃 提交于 2019-11-30 03:51:21
问题 I noticed a meaningful difference between iterating through a numpy array "directly" versus iterating through via the tolist method. See timing below: directly [i for i in np.arange(10000000)] via tolist [i for i in np.arange(10000000).tolist()] considering I've discovered one way to go faster. I wanted to ask what else might make it go faster? what is fastest way to iterate through a numpy array? 回答1: These are my timings on a slower machine In [1034]: timeit [i for i in np.arange(10000000)]

How to write a pager for Python iterators?

坚强是说给别人听的谎言 提交于 2019-11-30 03:15:04
问题 I'm looking for a way to "page through" a Python iterator. That is, I would like to wrap a given iterator iter and page_size with another iterator that would would return the items from iter as a series of "pages". Each page would itself be an iterator with up to page_size iterations. I looked through itertools and the closest thing I saw is itertools.islice. In some ways, what I'd like is the opposite of itertools.chain -- instead of chaining a series of iterators together into one iterator,

Python permutations with constraints

你。 提交于 2019-11-30 02:16:36
I am using python 3 and I am trying to find a way to get all the permutations of a list while enforcing some constraints. For instance, I have a list L=[1, 2, 3, 4, 5, 6, 7] I want to find all permutations. However, My constraints are: 1 should always come before 2. 3 should come before 4 which in turn should come before 5. Finally, 6 should come before 7. Of course, I can generate all permutations and ignore those which do not follow these constraints but this wouldn't be efficient I guess. This approach filters permutations using a simple filter. import itertools groups = [(1,2),(3,4,5),(6,7

where is the 'itertools' file

青春壹個敷衍的年華 提交于 2019-11-29 18:51:25
问题 import itertools print itertools#ok the code is ok but i can't find the itertools file. who can tell me where is the 'itertools file' my code is run python2.5 import itertools print itertools.__file__ Traceback (most recent call last): File "D:\zjm_code\mysite\zjmbooks\a.py", line 5, in <module> print itertools.__file__ AttributeError: 'module' object has no attribute '__file__' 回答1: >>> import itertools >>> itertools.__file__ '/usr/lib64/python2.6/lib-dynload/itertools.so' The fact that the

Determine all combinations of flipping a coin without using “itertools.product”

为君一笑 提交于 2019-11-29 17:46:00
I went through similar posts on the forum but all of them suggest using itertools.product but I was wondering if it can be solved without using it. I want to print all the combinations of outcomes for N flips of a coin. This can be done if N is known in advance. So the number of nested loops will be just N. But if N has to be determined dynamically ( input() function) then I am stuck in implementing it in code. In plain English it is easy to imagine that the number of for loops is proportional to N, but how do I implement it? Do I have to use lambdas or recursion? Below is as example code for