itertools

itertools product to generate all possible strings of size 3

ε祈祈猫儿з 提交于 2020-01-06 13:04:23
问题 Input: pos_1= 'AVNMHDRW' pos_2= 'KNTHDYBW' pos_3= 'KVNGSDRB' Trying to find all possible triplets using one item from pos_1, one from pos_2, and one from pos_3 I'm trying to figure out how to use itertools.product(*) but I'm a little confused Ultimately, I want to create a list (or generator object) of all the different possibilities by taking one from pos_1 then one from pos_2 and then one from pos_3 Example output: 'AKK','ANV','WWB' pos_1 stands for position one and so on for pos_2 and pos

How to iterate with range using itertools?

百般思念 提交于 2020-01-06 04:51:05
问题 Considering the following list within it 4 tuples: players_score = [ ('Joe', 100, 34, 38, 90, 67, 3, 10), ('Bob', 90, 38, 4, 100, 60, 4, 11), ('May', 80, 36, 40, 91, 70, 2, 12), ('Anna', 95, 32, 36, 92, 68, 8, 13) ] The players have been playing 7 games. In the first game, Joe has won with 100 points. I would like to score each player (of each game) according to the following: First/best player: 5 points Second player: 3 points Third player: 1 point Fourth player: -1 point -> But 0 points is

Fast/Efficient counting of list of space delimited strings in Python

自古美人都是妖i 提交于 2020-01-06 03:06:41
问题 Given the input: x = ['foo bar', 'bar blah', 'black sheep'] I could do this to get the count of each word in the list of space delimited string: from itertools import chain from collections import Counter c = Counter(chain(*map(str.split, x))) Or I could simple iterate through and get: c = Counter() for sent in x: for word in sent.split(): c[word]+=1 [out]: Counter({'bar': 2, 'sheep': 1, 'blah': 1, 'foo': 1, 'black': 1}) The question is which is more efficient if the input list of string is

Fast/Efficient counting of list of space delimited strings in Python

痴心易碎 提交于 2020-01-06 03:06:10
问题 Given the input: x = ['foo bar', 'bar blah', 'black sheep'] I could do this to get the count of each word in the list of space delimited string: from itertools import chain from collections import Counter c = Counter(chain(*map(str.split, x))) Or I could simple iterate through and get: c = Counter() for sent in x: for word in sent.split(): c[word]+=1 [out]: Counter({'bar': 2, 'sheep': 1, 'blah': 1, 'foo': 1, 'black': 1}) The question is which is more efficient if the input list of string is

Invertable Cartesian Product Elements/Index Translation Function

强颜欢笑 提交于 2020-01-05 09:14:07
问题 I have a problem where I need to identify the elements found at an indexed position within the Cartesian product of a series of lists but also, the inverse, i.e. identify the indexed position from a unique combination of elements from a series of lists. I've written the following code which performs the task reasonably well: import numpy as np def index_from_combination(meta_list_shape, index_combination ): list_product = np.prod(meta_list_shape) m_factor = np.cumprod([[l] for e,l in

summing nested dictionary entries

最后都变了- 提交于 2020-01-05 06:40:31
问题 I have a JSON file that I'm reading in as a dictionary. What I have is something like: "20101021": { "4x4": { "Central Spectrum": 5, "Full Frame": 5, "Custom": 1 }, "4x2": { "Central Spectrum": 5, "Full Frame": 5 }, "1x1": { "Central Spectrum": 5, "Full Frame": 4 }, }, "20101004": { "4x4": { "Central Spectrum": 5, "Full Frame": 5 }, "4x2": { "Central Spectrum": 5, "Full Frame": 5 }, "1x1": { "Central Spectrum": 5, "Full Frame": 5 } and so on. I am trying to calculate sums (over all dates) for

How to iterate the cartesian product so top items combine first?

一个人想着一个人 提交于 2020-01-05 03:55:12
问题 I need to get the cartesian product of iterables, like itertools.product gives me, but for optimization reasons I want those pairs/combinations with the lowest sum of indices to appear first. So, for example, if I have two lists, a = [1, 2, 3, 4, 5] and b = ['a', 'b', 'c', 'd', 'e'] , itertools.product gives me: >>> list(itertools.product(a, b)) [(1, 'a'), (1, 'b'), (1, 'c'), (1, 'd'), (1, 'e'), (2, 'a'), (2, 'b'), (2, 'c'), (2, 'd'), (2, 'e'), (3, 'a'), (3, 'b'), (3, 'c'), (3, 'd'), (3, 'e')

xrange versus itertools.count Python 2.7

穿精又带淫゛_ 提交于 2020-01-04 02:16:30
问题 I want to run a range from a start to an end value. It works fine on low numbers but when it gets too large it causes an overflow error as int too large to convert to C Long. I am using Python 2.7.3. I read here OverflowError Python int too large to convert to C long on using the itertools.count() method except that method works differently to the xrange method by stepping as opposed to declaring an end range value. Can the itertools.count() be set up to work like xrange() ? print "Range

Combine a list into tuple pairs (x, y)

China☆狼群 提交于 2020-01-04 01:22:25
问题 I'm trying to combine pairs of numbers passed in via sys.argv . Example: python myscript.py -35.12323 112.76767 -36.33345 112.76890 -33.68689 111.8980 My goal would be to turn these into sets of two's in tuples. Like such: ((-35.12323,112.76767),(-36.33345,112.76890),(-33.68689,111.8980)) This is what I've tried to so far, but it has a problem when I pass the results to the Shapely Point and Polygon methods. from shapely.geometry import Polygon from shapely.geometry import Point import sys

Combine a list into tuple pairs (x, y)

做~自己de王妃 提交于 2020-01-04 01:21:37
问题 I'm trying to combine pairs of numbers passed in via sys.argv . Example: python myscript.py -35.12323 112.76767 -36.33345 112.76890 -33.68689 111.8980 My goal would be to turn these into sets of two's in tuples. Like such: ((-35.12323,112.76767),(-36.33345,112.76890),(-33.68689,111.8980)) This is what I've tried to so far, but it has a problem when I pass the results to the Shapely Point and Polygon methods. from shapely.geometry import Polygon from shapely.geometry import Point import sys