python,algorithm,sorting,itertools

A simple solution that could speed it up a bit would be to store 1/z for each z in c in a list, and for each pair of x,y in a,b - use binary search for the highest 1/z (in the auxillary list) such that 1/x + 1/y + 1/z...

python,list,python-3.x,itertools,cartesian-product

Build a generator function which can yield as many values as you want and use it in the product, like this >>> from itertools import product >>> def get_chunks(items, number=3): ... for i in range(len(items) - number + 1): ... yield items[i: i + number] ... ... and then define...

This is based on stars and bars rather than combinations and permutations. Start with a standard stars and bars algorithm, a refinement of my previous work: def stars_and_bars(star='*', bar='|', star_count=8, bar_count=2): if star_count == 0: yield (bar,) * bar_count return if bar_count == 0: yield (star,) * star_count return for...

You can use enumerate: >>> a = [7, 5, 5, 4] >>> list(itertools.combinations(enumerate(a), 2)) [((0, 7), (1, 5)), ((0, 7), (2, 5)), ((0, 7), (3, 4)), ((1, 5), (2, 5)), ((1, 5), (3, 4)), ((2, 5), (3, 4))] >>> b = list((i,j) for ((i,_),(j,_)) in itertools.combinations(enumerate(a), 2)) >>> b [(0,...

python,iterator,generator,itertools

Suppose you have some iterable of pairs: a = zip(range(10), range(10)) IIUC what you're asking, you could generate independent iterators for the firsts and seconds using itertools.tee: xs, ys = itertools.tee(a) xs, ys = (x[0] for x in xs), (y[1] for y in ys) Note this will keep in memory...

Close! The index on the split list is one off, it should be 2. Try grouped = [list(g) for _, g in itertools.groupby(test, lambda x: x.split('_')[2])] ...

python,list,dictionary,permutation,itertools

You can use dictionary expression data = [['cups', 'cusp', 'cpus', 'cpsu', 'csup', 'cspu',], ['pups', 'pusp','upsp', 'upps', 'upsp', 'uspp']] result = {each[0]:each[1:] for each in data} print result Yields: {'pups': ['pusp', 'upsp', 'upps', 'upsp', 'uspp'], 'cups': ['cusp', 'cpus', 'cpsu', 'csup', 'cspu']} ...

python,pandas,aggregation,itertools

See this answer: Pandas - Cumulative sum and percentage on column? and this: http://pandas.pydata.org/pandas-docs/stable/basics.html#basics-dt-accessors and this: http://pandas.pydata.org/pandas-docs/stable/groupby.html Updated for Weekly Cumulative: df = pd.DataFrame(data) df.columns = ['Date','Profit'] df['Date'] = pd.to_datetime(df['Date']) df['weekofyear'] = df['Date'].dt.weekofyear df.reset_index('Date') df.sort_index(inplace=True) df['Weekly_Cum'] = df.groupby('weekofyear').cumsum() Output: Date Profit weekofyear Weekly_Cum 0...

python,for-loop,dictionary,itertools

The actual problem is that, instead of adding 1 to the values, you're doubling them: sco[i][1] += sco[i][1] And no matter how many times you add 0 to 0, it's still 0. To add 1, just use += 1. But you've got another problem, at least in the code you...

python,python-3.x,iterator,generator,itertools

What about: import 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)] lists = [list_a, list_b, list_c] for l in lists: l.insert(0, None) for x in itertools.product(*lists): print list(filter(None, x)) For those lists I get 60 elements, including an empty element. For reference, the index of...

python,python-2.7,python-3.x,combinations,itertools

You could combine combinations and chain.from_iterable: >>> from itertools import chain, combinations >>> a = [1,2,3] >>> n = 2 >>> cc = chain.from_iterable(combinations(a, i) for i in range(n, len(a)+1)) >>> list(cc) [(1, 2), (1, 3), (2, 3), (1, 2, 3)] chain.from_iterable here is flattening what the generator expression (combinations(a,...

python,list,combinations,itertools

Using zip, itertools.product and list comprehension: >>> import itertools >>> a = ['A','B','C','D'] >>> b = ['a','b','c','d'] # [x.lower() for x in a] >>> [list(x) for x in itertools.product(*zip(a, b))] [['A', 'B', 'C', 'D'], ['A', 'B', 'C', 'd'], ['A', 'B', 'c', 'D'], ['A', 'B', 'c', 'd'], ['A', 'b', 'C', 'D'],...

python,list,grouping,itertools

One way would be to do it in steps: >>> grouped = enumerate(groupby(seq, key=lambda x: x[1]), 1) >>> extracted = ((i, [g[0] for g in gg]) for i, (k,gg) in grouped) >>> final = [(i, len(x), x) for i,x in extracted] >>> final [(1, 2, ['aaa', 'bbb']), (2, 2, ['ccc',...

python,csv,for-loop,iterator,itertools

Thanks to @jonrsharpe, I came to question myself on some trick to code. Here is what I reached : class RewindableFile(file): def __init__(self, *args, **kwargs): nb_backup = kwargs.pop('nb_backup', 1) super(RewindableFile, self).__init__(*args, **kwargs) self._nb_backup = nb_backup self._backups = [] self._time_anchor = 0 def next(self): if self._time_anchor >= 0: item = super(RewindableFile,...

Use itertools.product(*list_of_sets).

python,csv,numpy,pandas,itertools

You can specify a date format string in the params to to_csv which will output your dates how you like, no need to extract/convert/add new columns etc. So load data using read_csv: df = pd.read_csv('mydata.csv', parse_dates=['timestamp'] In [15]: df Out[15]: minit timestamp open high low close 0 0 2009-02-23 17:32:00...

python,arrays,list,dictionary,itertools

You don't need itertools for this, you can do this using a list comprehension and zip(*): >>> lst = [[[1, 1, 1, 0, 0, 0, 0], [0, 1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6, 7], [2, 3, 4, 5, 6, 7, 8], [3, 4, 5,...

itertools.combinations_with_replacement is the function you seek. In [17]: i=itertools.combinations_with_replacement((1,2,3), 4) In [18]: next(i) Out[18]: (1, 1, 1, 1) In [19]: next(i) Out[19]: (1, 1, 1, 2) In [20]: next(i) Out[20]: (1, 1, 1, 3) In [21]: next(i) Out[21]: (1, 1, 2, 2) In [22]: If you want the set of...

python,python-3.x,numpy,genetic-algorithm,itertools

Your loop isn't executing because i and j are both 0, so range(i) and range(j) are empty. Also you can't assign a list [i,random] to an array value (np.empty defaults to np.float64). I've simply changed it to only store the random number, but if you really want to store a...

Your problem is that when you loop over xy, each value of l is a single element of your xy list, one of the tuples. The value of l in the first iteration of the loop is (3.625, 41.435), the second is (3.625, 42.435), and so on. When you do...

I approached the problem as follows: import functools def euler_014(max_=1000000): longest = max_len = None for n in range(1, max_): length = len_collatz(n) if max_len is None or length > max_len: max_len, longest = length, n return longest def memoize(f): cache = {} @functools.wraps(f) def func(*args): if args not in...

python,permutation,product,itertools,cartesian-product

Simple: itertools.product(pos_1, pos_2, pos_3) This can be iterated over; if you want a list, just pass it to list. What exactly is the issue? Edit: This produces tuples of the items from each source. If you want to join them back into strings, you can do that manually when you...

python,list,nested-lists,itertools,chain

You could keep unpacking until you hit tuples: def unpack_until(data, type_): for entry in data: if isinstance(entry, type_): yield entry else: yield from unpack_until(entry, type_) Then: >>> list(unpack_until(lllt, tuple)) [('ab', 'cd'), ('ef', 'gh'), ('ij', 'kl'), ('mn', 'op'), ('qr', 'st'), ('uv', 'w'), ('x', 'y'), ('z', 'foobar')] ...

python,list-comprehension,itertools

What about the simple a = [13 * i + 1 for i in range(24)] w = 3 aa = a + a[:w] print([sum(aa[i:i+w]) for i in range(len(a))]) Note that if the window is big there are better ways to compute a sliding window sum in O(n) (i.e. with a...

python,json,python-2.7,itertools

I've added headers to your input like: Store,Region,District,name,location 1234,90,910,MallA,GMT 4567,87,902,MallB,EST 2468,90,811,MallC,PST 1357,87,902,MallD,CST then used python csv reader and group by like this: import csv from itertools import groupby, ifilter from operator import itemgetter data = [] with open('in.csv') as csvfile: reader = csv.DictReader(csvfile) regions = [] regions_dict = sorted(list(reader), key=itemgetter('Region'))...

Using set intersection: import itertools import string numbers = set(range(10)) letters = set(string.ascii_letters) print([x for x in itertools.combinations([0,1,2,3,4,'a','b','c','d'], 4) if set(x) & letters and set(x) & numbers]) ...

python,loops,tuples,combinations,itertools

This works for me : for pair in itertools.combinations(string_list, 2): print(pair[0], pair[1]) And can also be written using tuple unpacking: for e1, e2 in itertools.combinations(string_list, 2): print(e1, e2) I'll let you adapt this to your code....

You can just do it in a for loop, one at a time: for a_perm in itertools.product(*elems): print(a_perm) itertools.product() gives you iterator, which you can iterate over one item at a time....

In my CPython, itertools is coded in C rather than Python: In [1]: import itertools In [2]: itertools.__file__ Out[2]: '.../lib/python2.7/lib-dynload/itertools.so' Generally, precompiled C tends to be faster than interpreted Python, so this could well be one reason....

Use itertools.product to produce cartesian product of items. from itertools import product list(product(xrange(5), xrange(3))) [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2), (3, 0), (3, 1), (3, 2), (4, 0), (4, 1), (4, 2)] In [13]: %timeit list(product(xrange(5), xrange(3))) 100000...

It looks like you want the roundrobin itertools recipe: def roundrobin(*iterables): "roundrobin('ABC', 'D', 'EF') --> A D E B F C" # Recipe credited to George Sakkis pending = len(iterables) nexts = cycle(iter(it).next for it in iterables) while pending: try: for next in nexts: yield next() except StopIteration: pending -=...

python,python-3.x,numpy,itertools

Have you tried itertools.product(*iterables)? Sounds like it's what you're looking for. The function takes as many iterables as you want and makes a Cartesian product. Here is an example: import itertools dimension1 = range(3) dimension2 = ['a'] dimension3 = ['hello', 'world'] for res in itertools.product(dimension1, dimension2, dimension3): print(*res) Output: 0...

python,text-files,itertools,printers

This happens because readlines method retains the final \n on each line of the file you're reading. Try this. import webbrowser from itertools import product with open('servers.txt') as infile: servers = list(map(str.strip, infile.readlines())) with open('PRINTERS_toDelete.txt') as infile: queues = list(map(str.strip,infile.readlines())) strTemp1 = 'cscript c:\\windows\\system32\\prnmngr.vbs -d -p %s -s %s\n' with...

Try product with 3 repeats: import itertools print [list(x) for x in itertools.product('AB', repeat=3)] Gives: [['A', 'A', 'A'], ['A', 'A', 'B'], ['A', 'B', 'A'], ['A', 'B', 'B'], ['B', 'A', 'A'], ['B', 'A', 'B'], ['B', 'B', 'A'], ['B', 'B', 'B']] Remember that using list comprehension you can always resort to: ab_str...

As the docs say, you are supposed to apply groupby to a list which is already sorted using the same key as groupby itself: key = lambda fv: fv[0].split('__')[0] groups = groupby(sorted(x, key=key), key=key) Then grouped_fields is: {u'My field one': [(u'My field one__AccountName', u'Lorem ipsum bla bla bla'), (u'My field...

Here is a version of product (for just two iterators), that tries to be as lazy as possible. def product(i1, i2): for a in i1: j2, i2 = itertools.tee(i2) for b in j2: yield (a, b) ...

python,list,collections,itertools

>>> thing = [lst[i:i+n] for i in range(0,len(lst),n)] >>> thing [['a', 'b'], ['c', 2], ['e']] Using a list comprehension...

python,dictionary,list-comprehension,product,itertools

I would do, manually iterating over the keys and then the values for each key: [(k, v) for k in email_fields for v in email_fields[k]] Output: [('txt', 'subject'), ('txt', 'plaintext_body'), ('html', 'html_body')] No need for itertools here. :)...

Instead of flattening after the fact, just build the list directly by concatenation: TargetArray = line[:6] + [AlleleNumber, AltAlleleCount,HQscoreindex] + GeneAnnotationFields ...

You want combinations, not product: teams_list = list(itertools.combinations(teams, 2)) ...

If I understand you correctly, you simply want to select an element from a list with a given probability. Why not use numpy? >>> x = {'event1': 0.1 , 'event2': 0.2 , 'event3': 0.2 , 'event4': 0.4, 'event5': 0.1} >>> events, p = zip(*x.items()) >>> np.random.choice(events, p=p, replace=False) 'event4' ...

Here's one way: In [14]: x = [9, 13, 10, 5, 3] In [15]: np.triu(np.outer(x, x), k=1).sum() Out[15]: 608 but I'd go with @user2357112's answer....

You can use itertools.permutations and then unpack the values, like this sum(a(*perm) for perm in permutations((j, j, k, l))) For example, >>> from itertools import permutations >>> from random import randint >>> def a(*args): ... return args[randint(0, len(args) - 1)] >>> sum(a(*perm) for perm in permutations((1, 2, 3, 4))) 57...

This question has been asked a couple of times already: Using numpy to build an array of all combinations of two arrays itertools product speed up The first link has a working numpy solution, that is claimed to be several times faster than itertools, though no benchmarks are provided. This...

Since you're just counting, the product isn't necessary This could be abbreviated to: lst = ['american', 'japanese'] candidates = ['{}{:02d}'.format(e[:6], i) for e in lst for i in range(100)] Which is alanalgous to the loop candidates = [] for e in lst: for i in range(100): candidates.append('{}{:02d}'.format(e[:6], i)) If really...

python,list,python-2.7,iteration,itertools

You need to use itertools.product to get the cartesian product, like this [dis.jaccrd(string1, string2) for string1, string2 in product(list_1, list_2)] The product will group the items, like this >>> from pprint import pprint >>> pprint(list(product(list_1, list_2))) [(['Hi my name is anon'], ['Hi my name is anon_2']), (['Hi my name is...

python,tree,generator,itertools

Chaining multiple chains results in a recursive functions call overhead proportional to the amount of chains chained together. First of all, our pure python chain implementation so that we won't lose stack info. The C implementation is here and you can see it does basically the same thing - calls...

python,arrays,string,itertools

This would be a nice way of doing that >>> from itertools import imap >>> ''.join(imap(str, item)) 012345 ...

python,list,combinatorics,itertools

You can do this with itertools.product and zip from itertools import product print [zip(A, item) for item in product(B, repeat=len(A))] Output [[('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',...

python,list,combinations,itertools

You can use itertools.combinations and itertools.product for this: >>> from itertools import combinations, product, chain, starmap >>> [p for c in combinations(a, 2) for p in product(*c)] [(1, 4), (1, 5), (1, 6), (1, 7), (2, 4), (2, 5), (2, 6), (2, 7), (3, 4), (3, 5), (3, 6), (3,...

The for loop creates an iterator for iterables; xrange() is a sequence (an iterable), not an iterator. Create the iterator explicitly, then advance that by using dropwhile() (iterating over that object!) to skip elements: cl = ['a', 'b', 'c', 'd', 'e'] rangeiter = iter(xrange(0, len(cl))) for x in rangeiter: print('{}-'.format(x)),...

python,python-2.7,iterator,itertools

Correct the code: chars = groupby(l, lambda x: x[1].isalpha()) result = [list(v) for k, v in chars if k] To figure out the weird output >>> l = list(enumerate(text)) >>> chars = groupby(l, lambda x: x[1].isalpha()) >>> list(chars.next()[1]) [(0, 'a'), (1, 'a'), (2, 'a')] >>> for k,v in list(chars): print...

Here is one possible implementation - I've added a set, named seen, inside the generator function to keep track of the elements we've already yielded. Note that this means that all elements in every iterable must be hashable (if they get reached), which is not a limitation of the base...

python,math,combinations,itertools

Always there are 2nā1 non-empty subsets of the set {1,...,n}. For example consider the list ['a','b','c']: >>> [list(combinations(['a','b','c'],i)) for i in range(1,4)] [[('a',), ('b',), ('c',)], [('a', 'b'), ('a', 'c'), ('b', 'c')], [('a', 'b', 'c')]] >>> l=[list(combinations(['a','b','c'],i)) for i in range(1,4)] >>> sum(map(len,l)) 7 That the length of our list is...

To handle the last element, you can use modulo: index % len(users). Here is one way: def find_after_name(users, name): for i, user in enumerate(users): if user.name == name: return users[(i+1) % len(users)] Another option would be to zip the list with a shifted copy of the list. deque.rotate() is useful...

Are you actually running the whole solve() function linked? It seems to me that you're trying to run only the for loop (the snippet that is a part of your question). That won't work if you don't run it as a part of a function. If this doesn't answer your...

itertools.combinations returns a generator and not a list. What this means is that you can iterate over it but not access it element by element with an index as you are attempting to. Instead you can get each combination like so: import itertools for combination in itertools.combinations([1,2,3], 2): print combination...

python,file,python-2.7,itertools

A for-loop over islice won't raise StopIteration, so no need to worry about that and islice takes care of EOF as well. So, at the end of the loop you can simply call a func to create xml file with data. And instead of looping over islice I'd suggest you...

python,arrays,list,python-2.7,itertools

How about this one-liner? from itertools import combinations, product, chain x = [['222', '219'], ['221'], ['220', '218', '216']] combos = list(chain.from_iterable( [list(product(a,b)) for (a,b) in combinations(x,2)])) Edit: roippi is absolutely correct. We can get rid of unnecessary intermediates by combos = list(chain.from_iterable(product(*c) for c in combinations(x,2))) ...

If you have many parameters, a constraint-based approach using a module like python-constraint may be easier to work with - let it do the hard work of figuring out which combinations are valid. This would look something like from constraint import Problem prob = Problem() prob.addVariables(["a1", "a2"], range(10,41,2)) prob.addVariable("b1", [0,...

An efficient way is to use sets. >>> set1= set(['apples', 'oranges']) >>> set2= set(['oranges', 'raisins']) >>> print len(set1.intersection(set2)) 1 So, basically: create a set for each topic's text for each topic, iterate each other topic and check the len of the intersection of their texts sets topic_text_sets= {topic:set(text) for topic,(text,_)...

python,big-o,time-complexity,itertools

First, it's always possible that you're going to find a hash collision before you find the right password. And, for a long-enough input string, this is guaranteed. So, really, the algorithm is constant time: it will complete in about 2^256 steps no matter what the input is. But this isn't...

python,hash,sha1,itertools,brute-force

Don't create the list just iterate over the itertools.product object getting one value at a time to avoid storing all in memory at once: for ele in itertools.product("0123456789abcdefghijklmnopqrstuvwxyz",repeat=6): print("".join(ele)) 000000 000001 000002 000003 000004 000005 000006 000007 000008 000009 00000a 00000b 00000c 00000d 00000e 00000f 00000g 00000h 00000i 00000j 00000k...

I suggest using zip with itertools.repeat. Probably this is simple enough to do inline (where you'll know the order, presumably), but if you want it in a function, you can do that too: def iter_intersperse(iterOver, injectItem, startWithIter = True): if startWithIter: return zip(iterOver, itertools.repeat(injectItem)) return zip(itertools.repeat(injectItem), iterOver) If you're using...

python,environment-variables,itertools,scopes

Python is not block scoped. Roughly speaking, Python is function scoped. In other languages, if you did for (int i = 0; i < whatever; i++) { ... } then i would be local to the for loop. In Python, the equivalent for i in xrange(whatever): uses an i variable...

python,list,order,combinations,itertools

I believe what you are looking for are all possible slices of your original list. Your desired output translated into slices is this: a # slices[0:1] b # slices[1:2] c # slices[2:3] d # slices[3:4] a b # slices[0:2] b c # slices[1:3] c d # slices[2:4] a b c...

size_of_group = 5 all_states = State.objects.order_by('-population') grouped_states = [all_states[i:i + size_of_group] for i in xrange(0, len(all_states), size_of_group)] It will give You a list of sublists. Each sublist contains size_of_group states. grouped_states[0] have 5 with biggest populations. '-population' says that the order is descending. Assuming Your model looks like: class State(models.Model)...

Looks like you want something like import itertools x = list(itertools.product(*d.values())) does this leave out anything you desire...?...

python,list,python-3.x,numpy,itertools

As a variant, fast and short from operator import lt from itertools import starmap, izip all(starmap(lt, izip(a, b))) ...

python,json,dictionary,nested,itertools

Maybe I misunderstood the expected final result, but you might not need counters... A simple sum could suffice if you know that you're only going to have two levels of nesting. Let's assume you loaded your json dictionary of dictionaries into a variable called data. Then you can do: results...

python,optimization,combinations,combinatorics,itertools

Yes: itertools.combinations print list(itertools.combinations(w, 2)) You mention this in your question - I'm not sure why you wouldn't look in the docs before asking on StackOverflow....

You are making string comparisons; '9' is greater than '11' because the character '9' comes later in the alphabet. You'll have to convert those to integers first. I'd use a dictionary to map prefixes to the maximum number: def find_latest(lst): prefixes = {} for entry in lst: code, value =...

You can do this using itertools.combinations_with_replacement: >>> L, N = 3,3 >>> cc = combinations_with_replacement(range(1, N+1), L) >>> for c in cc: print(c) (1, 1, 1) (1, 1, 2) (1, 1, 3) (1, 2, 2) (1, 2, 3) (1, 3, 3) (2, 2, 2) (2, 2, 3) (2, 3, 3)...

python,numpy,combinations,sympy,itertools

Assuming I understand your aim (not sure what you might want to happen in cases of duplicate elements, namely whether you want to consider them distinct or not), you could use itertools.product: import itertools def everywhere(seq, width): for locs in itertools.product(range(width), repeat=len(seq)): output = [[] for _ in range(width)] for...

I'm assuming you want them to line up as follows: a b c d e f g h i j k l ... 1 2 3 4 5 6 7 8 9 10 11 12 ... You should define a dictionary to do this, like you said. You can either...

python,permutation,itertools,dice,cartesian-product

As Calum noted, you should use the built-in itertools for these common loops. In your case, you would want: import itertools results = [sum(x) for x in itertools.product(range(1,5),repeat=9)] range(1,5) represents the 4 sides of the die repeat=9 represents the 9 dice you want to roll See itertools.product for doc...