FAQ Database Discussion Community


Cartesian product of a list of sets in python

python,itertools
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,...

Nested computation of Cartesian-product of dice rolls

python,permutation,itertools,dice,cartesian-product
I'm working on a Python program where I have to come up with all the ways to roll 9 4-sided dice. I've been trying to come up with a more concise way to write this line: for n in [sum([a, b, c, d, e, f, g, h, i]) for a...

Is there a way of avoiding so many list(chain(*list_of_list))?

python,list,nested-lists,itertools,chain
If I have a list of list of list of tuples of two strings. I want to flatten it out to a non-nested list of tuples, I could do this: >>> from itertools import chain >>> lst_of_lst_of_lst_of_tuples = [ [[('ab', 'cd'), ('ef', 'gh')], [('ij', 'kl'), ('mn', 'op')]], [[('qr', 'st'), ('uv',...

Matrix of variable size [i x j] (Python, Numpy)

python,python-3.x,numpy,genetic-algorithm,itertools
I am attempting to build a simple genetic algorithm that will optimize to an input string, but am having trouble building the [individual x genome] matrix (row n is individual n's genome.) I want to be able to change the population size, mutation rate, and other parameters to study how...

Python : defining function that returns summation of all permutations

python,itertools
I have a python function a(i,j,k,l). For 0<i<100, 0<j<100, 0<k<100, 0<l<100, i get a single value with this function. eg. a(1,4,5,3) = 5. Now what I want is a function b(i,j,k,l) that can give me the sum of a(i,j,k,l) for all the permutations of i,j,k,l. eg b(1,4,5,3) = a(1,4,5,3) +...

Numpy equivalent of itertools.product [duplicate]

python,numpy,itertools
This question already has an answer here: itertools product speed up 5 answers I know about itertools.product for iterating on a list of several dimensions of keywords. For instance if I have this: categories = [ [ 'A', 'B', 'C', 'D'], [ 'E', 'F', 'G', 'H'], [ 'I', 'J',...

Convert CSV to well-structured JSON in Python

python,json,python-2.7,itertools
I have a CSV file that is structured as below : Store, Region, District, MallName, Location 1234,90,910,MallA,GMT 4567,87,902,MallB,EST 2468,90,811,MallC,PST 1357,87,902,MallD,CST What I was able to accomplish with my iterative brow-beating was getting a format like so: { "90": { "910": { "1234": { "name": "MallA", "location": "GMT" } }, "811":...

How to do a rolling aggregation of data week wise in python?

python,pandas,aggregation,itertools
I have a dataset whose structure is : Date Profit A sample of the dataset is: Date Profit 2013-06-21 14 2013-06-22 19 2013-06-23 11 2013-06-24 13 2013-06-25 6 2013-06-26 22 2013-06-27 22 2013-06-28 3 2013-06-29 5 2013-06-30 10 2013-07-01 17 2013-07-02 14 2013-07-03 9 2013-07-04 7 Sample input is :...

'float' object is unsubscriptable

python,numpy,itertools,netcdf
thats what I get: TypeError: 'float' object is unsubscriptable Thats what I did: import numpy as N import itertools #I created two lists, containing large amounts of numbers, i.e. 3.465 lx = [3.625, 4.625, ...] ly = [41.435, 42.435, ...] #The lists are not the same size! xy = list(itertools.product(lx,ly))...

How can i create a permutation with a limit on specific characters efficently?

python,itertools
To give you an idea of what i'm talking about. This is the code i have now: chrs = 'ABCDEF1234567890' with open('two.txt', 'w') as two: for xs in itertools.product(chrs, repeat=10): h = sum(x.isalpha() for x in xs) if h == 2: two.write(''.join(xs) + '\n') Unfortunately this literally takes days as...

Dive Into Python3 | Alphametics | SyntaxError: 'return' outside function

python,python-3.x,itertools
This code snippet is from the Alphametics examples at DIP3 for guess in itertools.permutations(digits, len(characters)): if zero not in guess[:n]: equation = puzzle.translate(dict(zip(characters, guess))) if eval(equation): return equation I've used 4-spaced indentation as recommended by PEP3, but still keep getting the error SyntaxError: 'return' outside function Any help would be...

Chaining of Python generators to traverse a nested data structure; best practices

python,tree,generator,itertools
Suppose I have a nested data structure that I want to traverse. This data structure contains nodes which in turn may offer their children via node.get_children_generator(). Of course, these children are also of type node and are evaluated in a lazy manner, i.e. enumerated by a generator. For simplicity let...

How to calculate the number of all possible combinations for a range of numbers from 1 to N?

python,math,combinations,itertools
Other than doing this: from itertools import combinations def brute_force(x): for l in range (1,len(x)+1): for f in list(combinations(range(0,len(x)),l)): yield f x = range(1,18) len(list(brute_force(x))) [out]: 131071 How could I mathematically calculate the number of all possible combinations? Is there a way to do it computationally without enumerating the possible...

How to unzip an iterator?

python,iterator,generator,itertools
Given a list of pairs xys, the Python idiom to unzip it into two lists is: xs, ys = zip(*xys) If xys is an iterator, how can I unzip it into two iterators, without storing everything in memory?...

Every possible combination of a multi-list

python,python-3.x,iterator,generator,itertools
list_a = [("A","<",1), ("A","==",5)] list_b = [("B","<",5), ("B","==",7), ("B",">=",8)] list_c = [("C","<",10),("C","<=",6),("C",">",4),("C","<=",6)] I want to make a list of every possible combination with constraint of only one per list admissible. I can see that itertools.product is somewhat close to what I want, and I know that I could do something...

python itertools combinations logic

python,environment-variables,itertools,scopes
I'm looking at the code for the combinations on the python documentation for itertools (https://docs.python.org/2/library/itertools.html) def combinations(iterable, r): # combinations('ABCD', 2) --> AB AC AD BC BD CD # combinations(range(4), 3) --> 012 013 023 123 pool = tuple(iterable) n = len(pool) if r > n: return indices = range(r)...

Find object from list that has attribute equal to some value and also get the next object after

python,python-2.7,itertools
I am able to find a object that its attribute equals to some value. But I would like to also get the object after that from a list (and also if the found object is the last in list, the next object after that should be the first object). Something...

Grouping Django Model Instances

python,django,itertools
I've looked around and can't find anything exactly like this, so I'm asking... I have a database with all 50 US States and their populations are stored in an integer field. None of the populations are the same. I would like to create 10 groups of 5 states ordered by...

Grouping list of tuples in python

python,list,grouping,itertools
I have a list that consist of tuples and I already sorted this list based on 2nd item. Then I want to make my list grouped based on the 2nd item, and put 1st item into list. This is my input: [('aaa', 1), ('bbb', 1), ('ccc', 2), ('ddd', 2), ('eee',...

Combinations of elements in a dict in python

python,dictionary,itertools
Suppose I have a Ordered Dict of the form d = OrderedDict([('x1', ['x1_0', 'x1_1']), ('x2', ['x2_0', 'x2_1','x2_2'])]) How do I get combinations of the form [('x1_0', 'x2_0'),('x1_0', 'x2_1'),('x1_0', 'x2_2'),('x1_1', 'x2_0'),('x1_1', 'x2_1'),('x1_1', 'x2_2')] P.S. Here I am showing the result for only two variables here but I am looking for a...

How to bin all subsets of a python list into n bins

python,numpy,combinations,sympy,itertools
I have a list: a = range(2) and I am trying to get the list's contents binned into n(=3) bins, in all possible ways, giving (order not important): [[[],[0],[1]], [[],[1],[0]], [[],[0,1],[]], [[],[],[0,1]], [[0],[1],[]], [[0],[],[1]], [[1],[0],[]], [[1],[],[0]], [[0,1],[],[]]] So far, I have been using the sympy.utilities.iterables library, firstly to get all...

Making all possible 6 digit combinations of digits and alphabets

python,hash,sha1,itertools,brute-force
I am trying to do a brute force on one hash key i have , key is 28 bit and i have to find password which is 6 digit and includes 0-9 and a-z like 36 total possibilities for each place. I am using macbook with 16gb of ram but...

Cartesian product that returns outputs of varied lengths

python,list,python-3.x,itertools,cartesian-product
So I have these lists: a = [1, 2, 3] b = [11, 12, 13, 14] c = [21, 22, 23, 24, 25, 26] I want to get all possible combinations (duplicates are fine) consisting of 2 elements from a, 3 elements from b and 3 elements from c. Like...

Itertools and customizing list

python,itertools
So I have a list of teams: teams = ["Team A", "Team B", "Team C", "Team D",] # ... etc. What I'm trying to do is find all the possible match ups that that exist between these teams. I figured out I could do this: for x in itertools.product(teams,teams): teams_list.append([x[0],x[1]])...

Updating Values in Dictionary with for Loop of a List within a List

python,for-loop,dictionary,itertools
I know the logic/syntax of the code at the bottom of this post is off, but I'm having a hard time figuring out how I can write this to get the desired result. The first section creates this dictionary: sco = {'human + big': 0, 'big + loud': 0, 'big...

TypeError: 'itertools.combinations' object is not subscriptable

python,itertools
When I try to run: temp = (twoset2[x][i][0]-twoset[x][i][1]) I get: TypeError: 'itertools.combinations' object is not subscriptable My code: for x in range(0,64): for i in range(0,1): temp = (twoset2[x][i][0]-twoset[x][i][1]) DSET[counter2]= temp temp = 0 counter2 += 1 Basically what I am trying to do is: I have a list (twoset2)...

Split timestamp column into two new columns in CSV using python and pandas

python,csv,numpy,pandas,itertools
I have a large CSV file with over 210000 rows. I am new to python and pandas. I would like to efficently loop through the timestamp column, Split the timestamp column into 2 new columns (date and time) and then format the new date column to %Y%m%d and delete the...

swapping inside the list in python

python,dictionary,itertools
Suppose that I have two lists that contain the following elements. i.e. l1 = ['a','b','c'] l2 = [1,2,3] each element of l1 is related to the corresponding element in l2 at the same index. The relation is such that if the user enters the list l1 the output is l2.Suppose...

Append itertools.product() results to variable in Python

python,itertools
I am trying to append every possible two-digit combination to the end of each string in a list. The strings are each eight characters, and the digits should replace the seventh and eighth characters. I am using itertools.product() to generate these two-digit combos, but I am not sure how to...

How to compare two lists in python

python,list,python-3.x,numpy,itertools
Suppose I have two lists (or numpy.arrays): a = [1,2,3] b = [4,5,6] How can I check if each element of a is smaller than corresponding element of b at the same index? (I am assuming indices are starting from 0) i.e. at index 0 value of a = 1...

Classified permutations using itertools or numpy

python,python-3.x,numpy,itertools
I have a series of dimensions which can take discrete values. For instance, say I have 4 dimensions each containing a keyword from a dimension-specific list: color: black, blue, red, green, yellow size: xs, s, m, l, xl material: leather, fabric, cotton, wool gender: male, female I want to iterate...

Cyclical Sliding Window Iteration

python,list-comprehension,itertools
Consider some given sequence and a window length, say the list a = [13 * i + 1 for i in range(24)] (so that In [61]: a Out[61]: [1, 14, 27, 40, ..., 287, 300] ) and window length 3. I'd like to take the sliding window sum of this...

Problems computing the score of a pairwise list in an iterative way?

python,list,python-2.7,iteration,itertools
Let's suppose that I have the following lists (actually they have a lot of sublists): list_1 = [['Hi my name is anon'], ['Hi I like #hokey']] list_2 = [['Hi my name is anon_2'], ['Hi I like #Basketball']] I would like to compute the distance of all the possible pairwises with...

How to group related .tif files?

python,group,tiff,itertools
I am attempting to group related files associated with a .tif image. You can see from the list that there are 7 related files per group. I am looking for a way to group these files so that I can move them via shutil.move() into various folders. The following script...

Python — generator interspersing value between iterator — am I doing this correctly?

python,generator,itertools
I have this function I made: def iter_intersperse(iterOver, injectItem, startWithIter = True): for item in iterOver: sendItem = (item, injectItem) if startWithIter else (injectItem, item) yield sendItem to intersperse an item between items in a generator. It's for some wxpython AddMany calls and I want to add a spacer between...

Sum of products of pairs in a list

python,numpy,pandas,itertools
This is the problem I have. Given a list xList = [9, 13, 10, 5, 3] I would like to calculate for sum of each element multiplied by subsequent elements sum([9*13, 9*10, 9*5 , 9*3]) + sum([13*10, 13*5, 13*3]) + sum([10*5, 10*3]) + sum ([5*3]) in this case the answer...

maximum value of a unique string in a list

python,list,itertools
Hi I'm trying to make a list of the maximum value of a unique string within a list. example: a = ['DS10.json', 'DS11.json', 'DT4.json', 'DT5.json', 'DT6.json', 'CJ6.json', 'CJ7.json'] should return me a list of the following: ['DS11.json', 'DT6.json', 'CJ7.json'] I have tried the following code: def j(l): p = []...