itertools

Check if two nested lists are equivalent upon substitution

这一生的挚爱 提交于 2019-12-07 07:56:53
问题 For some context, I'm trying to enumerate the number of unique situations that can occur when calculating the Banzhaf power indices for four players, when there is no dictator and there are either four or five winning coalitions. I am using the following code to generate a set of lists that I want to iterate over. from itertools import chain, combinations def powerset(iterable): s = list(iterable) return chain.from_iterable(map(list, combinations(s, r)) for r in range(2, len(s)+1)) def

multiprocessing on tee'd generators

喜欢而已 提交于 2019-12-07 04:13:46
问题 Consider the following script in which I test two ways of performing some calculations on generators obtained by itertools.tee : #!/usr/bin/env python3 from sys import argv from itertools import tee from multiprocessing import Process def my_generator(): for i in range(5): print(i) yield i def double(x): return 2 * x def compute_double_sum(iterable): s = sum(map(double, iterable)) print(s) def square(x): return x * x def compute_square_sum(iterable): s = sum(map(square, iterable)) print(s) g1

Extract a list from itertools.cycle

别说谁变了你拦得住时间么 提交于 2019-12-07 04:12:01
问题 I have a class which contains a itertools.cycle instance which I would like to be able to copy. One approach (the only one I can come up with), is to extract the initial iterable (which was a list), and store the position that the cycle is at. Unfortunately I am unable to get hold of the list which I used to create the cycle instance, nor does there seem to be an obvious way to do it: import itertools c = itertools.cycle([1, 2, 3]) print dir(c) ['__class__', '__delattr__', '__doc__', '_

Python: find out whether a list of integers is coherent

风格不统一 提交于 2019-12-07 02:47:29
问题 I am trying to find out whether a list of integers is coherent or 'at one stretch', meaning that the difference between two neighboring elements must be exactly one and that the numbers must be increasing monotonically. I found a neat approach where we can group by the number in the list minus the position of the element in the list -- this difference changes when the numbers are not coherent. Obviously, there should be exactly one group when the sequence does not contain gaps or repetitions.

itertools does not recognize numpy ints as valid inputs on Python 3.6

亡梦爱人 提交于 2019-12-06 17:55:40
问题 Take this code: import itertools as it import numpy as np data = ['a','b','c','d'] dw = np.array([1, 3], dtype=np.int64) print(list(it.islice(data,dw[0],dw[1],1))) On Python 2.7 it prints ['b', 'c',] as expected. On Python 3.6 it throws an exception: ValueError: Stop argument for islice() must be None or an integer: 0 <= x <= sys.maxsize. The same goes for np.int32 , and other methods of the itertools package throw similar errors, e.g. when you use permutations you get TypeError: Expected int

Outputting all possible permutations of multiple lists

谁都会走 提交于 2019-12-06 16:28:12
I am very new to Python and have only just bought my first "Crashcourse in Python" book - originally my choice of language was PHP. My Objective: I desire a script that will output on-screen a list of all possible permutations of a particular pattern. Order is unimportant. The raw data and pattern (the dataset will not change): List1 = ['CA', 'CB', 'CC', 'CD', 'CE', 'CF', 'CG', 'CH', 'CJ', 'CK', 'CL', 'CM', 'CN', 'CO', 'CP', 'CR', 'CS', 'CT', 'CU', 'CV', 'CW', 'CX', 'CY'] List2 = ['51', '02', '52', '03', '53', '04', '54', '05', '55', '06', '56', '07', '57', '08', '58', '09', '59', '10', '60',

Take sequence of values from a python list

北城余情 提交于 2019-12-06 13:38:59
问题 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. 回答1: a = [3,2,5,7,4,5,6,3,8,4,5,7,8,9,5,7,8,4,9

Python itertools.combinations() memory problems

喜欢而已 提交于 2019-12-06 12:03:04
I'm processing a huge number of combinations of items (from League of Legends), about 72 million, all of which are fed into a function that calculates how beneficial they are. We're trying to find the best possible combination. Ignoring the fact that there might be better ways, algorithmically speaking, to do this, can anyone tell me why I'm getting a memory error? allpossiblei = itertools.combinations(items.keys(),5) maxc = 0 i = 0 for combo in allpossiblei: icombo = [items[name] for name in combo] res, tcost = calcStats(icombo, 0.658,100,100) if res > maxc : maxc = res print str(res) + " " +

python list group by first character

亡梦爱人 提交于 2019-12-06 11:10:54
list1=['hello','hope','hate','hack','bit','basket','code','come','chess'] What I need is: list2=[['hello','hope','hate','hack'],['bit','basket'],['code','come','chess']] If the first character is the same and is the same group, then sublist it. How can I solve this? You can use itertools.groupby : >>> from itertools import groupby >>> list1 = ['hello','hope','hate','hack','bit','basket','code','come','chess'] >>> [list(g) for k, g in groupby(list1, key=lambda x: x[0])] [['hello', 'hope', 'hate', 'hack'], ['bit', 'basket'], ['code', 'come', 'chess']] Expanding on TerryA's answer: To create a

Generating all possible combinations in a nested dictionary

て烟熏妆下的殇ゞ 提交于 2019-12-06 09:36:14
I need to test all possible installation configurations possible. The configurations are kept in a dictionary array that sometimes contains nested arrays. Here's a sample of the configuration information (actual configuration is much longer): config = {'database': 'sqlite', 'useExisting': False, 'userCredentials': {'authType': 'windows', 'user': r'.\Testing', 'password': 'testing' } } For database , options are ['sqlite','mysql','oracle'] , and for useExisting , the options are [True, False] . I can figure out how to go through all permutations of those. But for userCredentials , the options