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...

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',...

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',...

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...

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...

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...

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

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))...

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]])...

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...

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',...

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...

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...

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...

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...

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...

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)...

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":...

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...

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...

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...

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...

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)...

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...

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 = []...

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) +...

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...

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...

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...

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?...

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...

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...

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...

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...

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...

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...

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 :...