itertools

How to group similar items in a list?

与世无争的帅哥 提交于 2019-12-04 23:49:04
I am looking to group similar items in a list based on the first three characters in the string. For example: test = ['abc_1_2', 'abc_2_2', 'hij_1_1', 'xyz_1_2', 'xyz_2_2'] How can I group the above list items into groups based on the first grouping of letters (e.g. 'abc' )? The following is the intended output: output = {1: ('abc_1_2', 'abc_2_2'), 2: ('hij_1_1',), 3: ('xyz_1_2', 'xyz_2_2')} or output = [['abc_1_2', 'abc_2_2'], ['hij_1_1'], ['xyz_1_2', 'xyz_2_2']] I have tried using itertools.groupby to accomplish this without success: >>> import os, itertools >>> test = ['abc_1_2', 'abc_2_2',

Does python have a built-in function for interleaving generators/sequences?

梦想的初衷 提交于 2019-12-04 23:48:01
I noticed that itertools does not (it seems to me) have a function capable of interleaving elements from several other iterable objects (as opposed to zipping them): def leaf(*args): return (it.next() for it in cycle(imap(chain,args))) tuple(leaf(['Johann', 'Sebastian', 'Bach'], repeat(' '))) => ('Johann', ' ', 'Sebastian', ' ', 'Bach', ' ') (Edit) The reason I ask is because I want to avoid unnecessary zip/flatten occurrences. Obviously, the definition of leaf is simple enough, but if there is a predefined function that does the same thing, I would prefer to use that, or a very clear

Unexpected Behavior of itertools.groupby

这一生的挚爱 提交于 2019-12-04 22:55:04
问题 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?

python no output when using pool.map_async

邮差的信 提交于 2019-12-04 19:36:41
I am experiencing very strange issues while working with the data inside my function that gets called by pool.map. For example, the following code works as expected... import csv import multiprocessing import itertools from collections import deque cur_best = 0 d_sol = deque(maxlen=9) d_names = deque(maxlen=9) **import CSV Data1** def calculate(vals): #global cur_best sol = sum(int(x[2]) for x in vals) names = [x[0] for x in vals] print(", ".join(names) + " = " + str(sol)) def process(): pool = multiprocessing.Pool(processes=4) prod = itertools.product(([x[2], x[4], x[10]] for x in Data1))

Take sequence of values from a python list

眉间皱痕 提交于 2019-12-04 18:10:19
I have a array like this, a = [3,2,5,7,4,5,6,3,8,4,5,7,8,9,5,7,8,4,9,7,6] and I want to make list of values that are lesser than 7 (look like following) b = [[3,2,5],[4,5,6,3],[4,5],[5],[4],[6]] So I used following method, >>> from itertools import takewhile >>> a = [3,2,5,7,4,5,6,3,8,4,5,7,8,9,5,7,8,4,9,7,6] >>>list(takewhile(lambda x: x < 7 , a)) [3, 2, 5] But I only get the first sequence. Can anyone help me to solve this problem ? Thank you. a = [3,2,5,7,4,5,6,3,8,4,5,7,8,9,5,7,8,4,9,7,6] from itertools import groupby [list(g) for k, g in groupby(a, lambda x:x<7) if k] Output: [[3, 2, 5],

PyObjC + Xcode 3.2 + Non-Apple Python

回眸只為那壹抹淺笑 提交于 2019-12-04 17:09:34
I want to get started trying to develop a few simple applications with PyObjC. I installed PyObjC and the Xcode templates. I know that PyObjC itself works, since I've run this script successfully. When I tried to create a project from the Cocoa-Python Application template and ran it, I got this error: Traceback (most recent call last): File "main.py", line 10, in import objc File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/PyObjC/objc/__init__.py", line 25, in from _convenience import * File "/opt/local/Library/Frameworks/Python.framework/Versions/2

zip iterators asserting for equal length in python

旧时模样 提交于 2019-12-04 16:07:46
问题 I am looking for a nice way to zip several iterables raising an exception if the lengths of the iterables are not equal. In the case where the iterables are lists or have a len method this solution is clean and easy: def zip_equal(it1, it2): if len(it1) != len(it2): raise ValueError("Lengths of iterables are different") return zip(it1, it2) However, if it1 and it2 are generators, the previous function fails because the length is not defined TypeError: object of type 'generator' has no len() .

Separating a String

北战南征 提交于 2019-12-04 15:31:16
问题 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

Best way to enumerate a cartesian product with labels in python?

核能气质少年 提交于 2019-12-04 15:02:29
Given a dictionary mapping variables to possible outcomes: { 'lblA' : [False, True], 'lblB' : [False, True], 'lblC' : [0,1,2] } I want to enumerate all possible dictionary outcomes: [ { 'lblA' : False , 'lblB' : False, 'lblC' : 0 }, { 'lblA' : True , 'lblB' : False, 'lblC' : 0 }, { 'lblA' : False , 'lblB' : True, 'lblC' : 0 }, { 'lblA' : True , 'lblB' : True, 'lblC' : 0 }, { 'lblA' : False , 'lblB' : False, 'lblC' : 1 }, { 'lblA' : True , 'lblB' : False, 'lblC' : 1 }, { 'lblA' : False , 'lblB' : True, 'lblC' : 1 }, { 'lblA' : True , 'lblB' : True, 'lblC' : 1 }, { 'lblA' : False , 'lblB' :

itertools: Cartesian product of permutations

你离开我真会死。 提交于 2019-12-04 12:06:17
Using pythons itertools , I'd like to create an iterator over the outer product of all permutations of a bunch of lists. An explicit example: import itertools A = [1,2,3] B = [4,5] C = [6,7] for x in itertools.product(itertools.permutations(A),itertools.permutations(B),itertools.permutations(C)): print x While this works, I'd like to generalize it to an arbitrary list of lists. I tried: for x in itertools.product(map(itertools.permutations,[A,B,C])): print x but it did not do what I intended. The expected output is: ((1, 2, 3), (4, 5), (6, 7)) ((1, 2, 3), (4, 5), (7, 6)) ((1, 2, 3), (5, 4), (6