itertools

All possible ways to interleave two strings

丶灬走出姿态 提交于 2019-12-03 20:36:51
问题 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

Unexpected Behavior of itertools.groupby

血红的双手。 提交于 2019-12-03 15:25:10
This is the observed behavior: In [4]: x = itertools.groupby(range(10), lambda x: True) In [5]: y = next(x) In [6]: next(x) --------------------------------------------------------------------------- StopIteration Traceback (most recent call last) <ipython-input-6-5e4e57af3a97> in <module>() ----> 1 next(x) StopIteration: In [7]: y Out[7]: (True, <itertools._grouper at 0x10a672e80>) In [8]: list(y[1]) Out[8]: [9] The expected output of list(y[1]) is [0,1,2,3,4,5,6,7,8,9] What's going on here? I observed this on cpython 3.4.2 , but others have seen this with cpython 3.5 and IronPython 2.9.9a0

Failing to import itertools in Python 3.5.2

て烟熏妆下的殇ゞ 提交于 2019-12-03 12:07:51
I am new to Python. I am trying to import izip_longest from itertools. But I am not able to find the import "itertools" in the preferences in Python interpreter. I am using Python 3.5.2. It gives me the below error- from itertools import izip_longest ImportError: cannot import name 'izip_longest' Please let me know what is the right course of action. I have tried Python 2.7 too and ended up with same problem. Do I need to use lower version Python. Martijn Pieters izip_longest was renamed to zip_longest in Python 3 (note, no i at the start), import that instead: from itertools import zip

Why is itertools.chain faster than a flattening list comprehension?

﹥>﹥吖頭↗ 提交于 2019-12-03 11:11:00
In the context of a discussion in the comments of this question it was mentioned that while concatenating a sequence of strings simply takes ''.join([str1, str2, ...]) , concatenating a sequence of lists would be something like list(itertools.chain(lst1, lst2, ...)) , although you can also use a list comprehension like [x for y in [lst1, lst2, ...] for x in y] . What surprised me is that the first method is consistently faster than the second: import random import itertools random.seed(100) lsts = [[1] * random.randint(100, 1000) for i in range(1000)] %timeit [x for y in lsts for x in y] # 39

Separating a String

折月煮酒 提交于 2019-12-03 09:38:46
Given a string, I want to generate all possible combinations. In other words, all possible ways of putting a comma somewhere in the string. For example: input: ["abcd"] output: ["abcd"] ["abc","d"] ["ab","cd"] ["ab","c","d"] ["a","bc","d"] ["a","b","cd"] ["a","bcd"] ["a","b","c","d"] I am a bit stuck on how to generate all the possible lists. Combinations will just give me lists with length of subset of the set of strings, permutations will give all possible ways to order. I can make all the cases with only one comma in the list because of iterating through the slices, but I can't make cases

itertools.accumulate() versus functools.reduce()

扶醉桌前 提交于 2019-12-03 01:53:21
In Python 3.3, itertools.accumulate() , which normally repeatedly applies an addition operation to the supplied iterable, can now take a function argument as a parameter; this means it now overlaps with functools.reduce() . With a cursory look, the main differences between the two now would seem to be: accumulate() defaults to summing but doesn't let you supply an extra initial condition explicitly while reduce() doesn't default to any method but does let you supply an initial condition for use with 1/0-element sequences, and accumulate() takes the iterable first while reduce() takes the

How to stream in and manipulate a large data file in python

ぐ巨炮叔叔 提交于 2019-12-03 00:26:12
I have a relatively large (1 GB) text file that I want to cut down in size by summing across categories: Geography AgeGroup Gender Race Count County1 1 M 1 12 County1 2 M 1 3 County1 2 M 2 0 To: Geography Count County1 15 County2 23 This would be a simple matter if the whole file could fit in memory but using pandas.read_csv() gives MemoryError . So I have been looking into other methods, and there appears to be many options - HDF5? Using itertools (which seems complicated - generators?) Or just using the standard file methods to read in the first geography (70 lines), sum the count column,

Why does this assigned object share the same memory space as the original object?

北慕城南 提交于 2019-12-02 16:09:54
问题 In python I came across this strange phenomena while working with itertools groupby module. In python, variable assignment means assigning the new variable its own memory instead of a pointer to the original memory (from my understanding if this is incorrect please let me know): y = 7 x = y y = 9 x will still be 7 Yet when I was working with groupby module, I was using this module to group items that had the same key into one group. I wanted two groups since reiterating over the original

Multiprocessing an iterable in python

泪湿孤枕 提交于 2019-12-02 11:45:09
问题 I am trying to split the following code to allow for multiprocessing in python and it is really becoming a frustrating task for me - I am new to multiprocessing and have read the documentation and as many samples as I could find but still have not found a solution that will have it work on all cpu cores at one time. I would like to split the iterables into quarters and have it compute the test in parrallel. My single thread example: import itertools as it import numpy as np wmod = np.array([

Breadth-first version of itertools.chain()

最后都变了- 提交于 2019-12-02 11:31:12
问题 In itertools there's chain , which combines multiple generators in a single one, and in essence does a depth-first iteration over them, i.e., chain.from_iterable(['ABC', '123']) yields A, B, C, 1, 2, 3. But, there's no breadth-first version, or am I missing something? There's of course izip_longest , but for large numbers of generators this feels awkward, as the tuples will be very long and possibly very sparse. I came up with the following: def chain_bfs(*generators): generators = list