itertools

Using 'reduce' on a list of dictionaries

妖精的绣舞 提交于 2019-12-01 04:23:34
I'm trying to write a simple Python function that sums all values that have the key as likes . I'm working with functional programming for this assignment. Thus, I am required to use either a list-comprehension , map , filter , or reduce . In this case, I see reduce as a reasonable option. def sum_favorites(msgs): num_favorites = reduce(lambda x, y: x["likes"] + y["likes"], msgs) return num_favorites content1 = {"likes": 32, ...} content2 = {"likes": 8, ...} content3 = {"likes": 16, ...} contents = [content1, content2, content3] print(sum_favorites(contents)) The issue comes to when I actually

What should itertools.product() yield when supplied an empty list?

纵饮孤独 提交于 2019-12-01 03:43:14
I guess it's an academic question, but the second result does not make sense to me. Shouldn't it be as thoroughly empty as the first? What is the rationale for this behavior? from itertools import product one_empty = [ [1,2], [] ] all_empty = [] print [ t for t in product(*one_empty) ] # [] print [ t for t in product(*all_empty) ] # [()] Updates Thanks for all of the answers -- very informative. Wikipedia's discussion of the Nullary Cartesian Product provides a definitive statement: The Cartesian product of no sets ... is the singleton set containing the empty tuple. And here is some code you

Python - get all combinations of a list

邮差的信 提交于 2019-12-01 03:11:19
I know that I can use itertools.permutation to get all permutation of size r. But, for itertools.permutation([1,2,3,4],3) it will return (1,2,3) as well as (1,3,2) . I want to filter those repetitions (i.e obtain combinations) Is there a simple way to get all permutations (of all lengths)? How can I convert itertools.permutation() result to a regular list? Use itertools.combinations and a simple loop to get combinations of all size. combinations return an iterator so you've to pass it to list() to see it's content(or consume it). >>> from itertools import combinations >>> lis = [1, 2, 3, 4]

Aggregate all dataframe row pair combinations using pandas

痞子三分冷 提交于 2019-12-01 03:04:27
问题 I use python pandas to perform grouping and aggregation across data frames, but I would like to now perform specific pairwise aggregation of rows (n choose 2, statistical combination). Here is the example data, where I would like to look at all pairs of genes in [mygenes]: import pandas import itertools mygenes=['ABC1', 'ABC2', 'ABC3', 'ABC4'] df = pandas.DataFrame({'Gene' : ['ABC1', 'ABC2', 'ABC3', 'ABC4','ABC5'], 'case1' : [0,1,1,0,0], 'case2' : [1,1,1,0,1], 'control1':[0,0,1,1,1],

Using 'reduce' on a list of dictionaries

寵の児 提交于 2019-12-01 01:29:35
问题 I'm trying to write a simple Python function that sums all values that have the key as likes . I'm working with functional programming for this assignment. Thus, I am required to use either a list-comprehension , map , filter , or reduce . In this case, I see reduce as a reasonable option. def sum_favorites(msgs): num_favorites = reduce(lambda x, y: x["likes"] + y["likes"], msgs) return num_favorites content1 = {"likes": 32, ...} content2 = {"likes": 8, ...} content3 = {"likes": 16, ...}

Why itertools.groupby() doesn't work? [duplicate]

青春壹個敷衍的年華 提交于 2019-11-30 21:06:57
问题 This question already has answers here : itertools.groupby() not grouping correctly (3 answers) Closed last year . I've checked some topics about groupby() but I don't get what's wrong with my example: students = [{'name': 'Paul', 'mail': '@gmail.com'}, {'name': 'Tom', 'mail': '@yahoo.com'}, {'name': 'Jim', 'mail': 'gmail.com'}, {'name': 'Jules', 'mail': '@something.com'}, {'name': 'Gregory', 'mail': '@gmail.com'}, {'name': 'Kathrin', 'mail': '@something.com'}] key_func = lambda student:

itertools.product eliminating repeated elements

人盡茶涼 提交于 2019-11-30 21:02:52
How can I skip the tuples which has duplicate elements in the iteration when I use itertools.product ? Or let's say, is there anyway not to look at them in the iteration? Because skipping may be time consuming if the number of lists are too much. Example, lis1 = [1,2] lis2 = [2,4] lis3 = [5,6] [i for i in product(lis1,lis2,lis3)] should be [(1,2,5), (1,2,6), (1,4,5), (1,4,6), (2,4,5), (2,4,6)] It will not have (2,2,5) and (2,2,6) since 2 is duplicate in here. How can I do that? itertools generally works on unique positions within inputs, not on unique values . So when you want to remove

what is the quickest way to iterate through a numpy array

喜欢而已 提交于 2019-11-30 19:02:38
I noticed a meaningful difference between iterating through a numpy array "directly" versus iterating through via the tolist method. See timing below: directly [i for i in np.arange(10000000)] via tolist [i for i in np.arange(10000000).tolist()] considering I've discovered one way to go faster. I wanted to ask what else might make it go faster? what is fastest way to iterate through a numpy array? These are my timings on a slower machine In [1034]: timeit [i for i in np.arange(10000000)] 1 loop, best of 3: 2.16 s per loop If I generate the range directly (Py3 so this is a genertor) times are

How to write a pager for Python iterators?

两盒软妹~` 提交于 2019-11-30 18:44:55
I'm looking for a way to "page through" a Python iterator. That is, I would like to wrap a given iterator iter and page_size with another iterator that would would return the items from iter as a series of "pages". Each page would itself be an iterator with up to page_size iterations. I looked through itertools and the closest thing I saw is itertools.islice . In some ways, what I'd like is the opposite of itertools.chain -- instead of chaining a series of iterators together into one iterator, I'd like to break an iterator up into a series of smaller iterators. I was expecting to find a paging

Iterating over multiple indices with i > j ( > k) in a pythonic way

雨燕双飞 提交于 2019-11-30 18:14:34
i need to iterate over a tuple of indices. all indices must be in the range [0, N) with the condition i > j . The toy example I present here deals with only two indices; I will need to extend that to three (with i > j > k ) or more. The basic version is this: N = 5 for i in range(N): for j in range(i): print(i, j) and it works just fine; the output is 1 0 2 0 2 1 3 0 3 1 3 2 4 0 4 1 4 2 4 3 I don't want to have one more indentation level for every additional index, therefore I prefer this version: for i, j in ((i, j) for i in range(N) for j in range(i)): print(i, j) this works perfectly well,