itertools

How to call a builtin function (or method) from C code of a Python extension module?

家住魔仙堡 提交于 2019-12-02 08:43:17
问题 What I currently want to accomplish is to tweak Pythons itertools module function combinations to sort the passed iterable before creating combinations out of it with the goal of having the returned combinations sorted. I am working on a Python extension module for the first time and my only experience up to now was to write and compile a "Hello World" like Python extension module, but I hope that my overall programming experience in a couple of programming languages is a solid enough

Iterate through sum combinations in Python 3

倾然丶 夕夏残阳落幕 提交于 2019-12-02 08:13:17
I'm look for a way to find all combinations of sums with elements of a Fibonacci sequence with a given limit which equal that same value. I know that combinations() from itertools is our best bet in solving such problems, but as I'm new in Python I like to know how I can retain the matching combinations (as only one is correct, as this is not the end of the algorithm). I currently have: # Call Fibonacci sequence with a given limit: def fib1(n): result = [] a, b = 1, 1 while a < n: result.append(a) a, b = b, a + b return result # Wrong code, skeleton: def zeckendorf(n): from itertools import

Disturbing odd behavior/bug in Python itertools groupby?

最后都变了- 提交于 2019-12-02 06:54:22
问题 I am using itertools.groupby to parse a short tab-delimited textfile. the text file has several columns and all I want to do is group all the entries that have a particular value x in a particular column. The code below does this for a column called name2 , looking for the value in variable x . I tried to do this using csv.DictReader and itertools.groupby . In the table, there are 8 rows that match this criteria so 8 entries should be returned. Instead groupby returns two sets of entries, one

Multiprocessing an iterable in python

一笑奈何 提交于 2019-12-02 04:52:29
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([[0,1,2],[3,4,5],[6,7,3]]) pmod = np.array([[0,1,2],[3,4,5],[6,7,3]]) plines1 = it.product(wmod[0],wmod[1

Breadth-first version of itertools.chain()

霸气de小男生 提交于 2019-12-02 04:34:49
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(generators) while generators: g = generators.pop(0) try: yield g.next() except StopIteration: pass else:

Cartesian product of a list of sets in python

笑着哭i 提交于 2019-12-02 04:22:09
问题 I had a list of sets. I do not know the length of the list apriori. I wanted to find the Cartesian product of the sets in the list in some code I'm writing. For example: I have list_of_sets=[set(['A']),set(['A','B','C']), set('D','E')]; I want to output a cartesian product of these sets, which would be, ('A', 'A', 'E') ('A', 'A', 'D') ('A', 'C', 'E') ('A', 'C', 'D') ('A', 'B', 'E') ('A', 'B', 'D') If I knew that the list had size 3 in advance, I could use the following code to generate this

Cartesian product giving a dictionary

冷暖自知 提交于 2019-12-02 02:04:29
I have the following lists: brand=["Audi","Mercedes"] speed=[130,150] model=["sport","family"] I want to obtain the equivalent of: ll=[] ll.append({'brand':'mercedes', 'speed':130, 'model':'family'}) ll.append({'brand':'mercedes', 'speed':130, 'model':'sport'}) ll.append({'brand':'audi', 'speed':130, 'model':'family'}) ll.append({'brand':'audi', 'speed':130, 'model':'sport'}) ll.append({'brand':'mercedes', 'speed':150, 'model':'family'}) ll.append({'brand':'mercedes', 'speed':150, 'model':'sport'}) ll.append({'brand':'audi', 'speed':150, 'model':'family'}) ll.append({'brand':'audi', 'speed'

Can this python code be more efficient?

丶灬走出姿态 提交于 2019-12-01 21:44:12
I have written some code to find how many substrings of a string are anagram pairs. The function to find anagram(anagramSolution) is of complexity O(N). The substring function has complexity less than N square. But, this code here is the problem. Can it be more optimized? for i in range(T): x = raw_input() alist = get_all_substrings(x) for k, j in itertools.combinations(alist,2): if(len(k) == len(j)): if(anagramSolution(k,j)): counter +=1 counterlist.append(counter) counter = 0 The alist can have thousands of items (subsets). The main problem is the loop. It is taking a lot of time to iterate

izip_longest in itertools: How does rasing IndexError inside the iterator work?

China☆狼群 提交于 2019-12-01 21:35:26
问题 In this question @lazyr asks how the following code of izip_longest iterator from here works: def izip_longest_from_docs(*args, **kwds): # izip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D- fillvalue = kwds.get('fillvalue') def sentinel(counter = ([fillvalue]*(len(args)-1)).pop): yield counter() # yields the fillvalue, or raises IndexError fillers = repeat(fillvalue) iters = [chain(it, sentinel(), fillers) for it in args] try: for tup in izip(*iters): yield tup except IndexError: pass

In Python 3.x, why is there not an itertools shared-object on disk?

a 夏天 提交于 2019-12-01 19:38:04
Is the itertools C module included somehow in the main Python binary in 3.x? Assuming that the C module is built and included, which it appears to be: >>> import inspect >>> import itertools >>> >>> inspect.getsourcefile(itertools) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/Cellar/python3/3.4.3_2/Frameworks/Python.framework/Versions/3.4/lib/python3.4/inspect.py", line 571, in getsourcefile filename = getfile(object) File "/usr/local/Cellar/python3/3.4.3_2/Frameworks/Python.framework/Versions/3.4/lib/python3.4/inspect.py", line 518, in getfile raise