itertools

PyObjC + Xcode 3.2 + Non-Apple Python

↘锁芯ラ 提交于 2019-12-06 09:09:33
问题 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",

Conditional Cartesian product of lists in itertools

醉酒当歌 提交于 2019-12-06 08:47:56
I have four lists: LISTA = ['A1', 'A2'] LISTB = ['B1_C', 'B2_D'] LISTC = ['C1', 'C2'] LISTD = ['D1', 'D2'] I'd like to get the Cartesian product of LISTA and LISTB , and then depending on the value of B, I'd like to add either the product of C, or the product of D. (A1 B1_C C1) (A1 B1_C C2) (A2 B1_C C1) (A2 B1_C C2) (A1 B2_D D1) (A1 B2_D D2) (A2 B2_D D1) (A2 B2_D D2) I can get the first part with itertools.product(LISTA, LISTB) , but I've been looking through itertools for how to achieve the second part and I'm not sure the best way to go. Suggestions? Here is an interactive demonstration of a

Combination of 1 and 0 in an array in Python

只谈情不闲聊 提交于 2019-12-06 04:19:49
I want to make a combination of 1's and 0's in a 2d array like the following: [[ 1, 1, 1, 1, 0, 0, 0, 0 ], [ 1, 1, 1, 0, 1, 0, 0, 0 ], [ 1, 1, 1, 0, 0, 1, 0, 0 ], [ 1, 1, 1, 0, 0, 0, 1, 0 ], [ 1, 1, 1, 0, 0, 0, 0, 1 ], . . . ] That means a combination of four 1's and four 0's. I have looked at the itertools module's permutations() and combinations() , but couldn't find any suitable functions to perform this combination. You can also use combinations to generate unique combinations directly: n = 8 n1 = 4 for x in itertools.combinations( xrange(n), n1 ) : print [ 1 if i in x else 0 for i in

How to find the maximum product of two elements in a list?

梦想与她 提交于 2019-12-06 03:57:24
问题 I was trying out a problem on hackerrank contest for fun, and there came this question. I used itertools for this, here is the code: import itertools l = [] for _ in range(int(input())): l.append(int(input())) max = l[0] * l[len(l)-1] for a,b in itertools.combinations(l,2): if max < (a*b): max = (a*b) print(max) Is their any other efficient way than this? As I am getting time out error on some test cases which I cant access (as its a small contest). 回答1: Here is an implementation following

Using itertools for recursive function application

强颜欢笑 提交于 2019-12-06 01:48:19
问题 I need a Python function iterate(f, x) that creates an iterator returning the values x, f(x), f(f(x)), f(f(f(x))), etc (like, e.g., Clojure's iterate). First of all, I was wondering: Does this already exist somewhere in the standard library and I'm only missing it? Of course it's easy enough to implement with a generator: def iterate(f, x): while True: yield x x = f(x) Just out of curiosity: Is there a more functional way to do this in Python, e.g. with some itertools or functools magic? In

Count consecutive ones in a dataframe and get indices where this occurs

孤者浪人 提交于 2019-12-06 01:44:30
I have a pandas.DataFrame with integer column names, which has zeroes and ones. An example of the input: 12 13 14 15 1 0 0 1 0 2 0 0 1 1 3 1 0 0 1 4 1 1 0 1 5 1 1 1 0 6 0 0 1 0 7 0 0 1 1 8 1 1 0 1 9 0 0 1 1 10 0 0 1 1 11 1 1 0 1 12 1 1 1 1 13 1 1 1 1 14 1 0 1 1 15 0 0 1 1 I need to count all consecutive ones which has a length/sum which is >=2, iterating through columns and returning also indices where an array of the consecutive ones occurs (start, end). The preferred output would be a 3D DataFrame, where subcolumns "count" and "indices" refer to integer column names from the input. An

Explain combination function of python module itertools

☆樱花仙子☆ 提交于 2019-12-06 00:55:27
I have often used itertools module in Python but it feels like cheating if I don't know the logic behind it. Here is the code to find combinations of string when order is not important. def combinations(iterable, r): # combinations('ABCD', 2) --> AB AC AD BC BD CD # combinations(range(4), 3) --> 012 013 023 123 pool = tuple(iterable) n = len(pool) if r > n: return indices = list(range(r)) yield tuple(pool[i] for i in indices) while True: for i in reversed(range(r)): if indices[i] != i + n - r: break else: return indices[i] += 1 for j in range(i+1, r): indices[j] = indices[j-1] + 1 yield tuple

Finding groups of increasing numbers in a list

僤鯓⒐⒋嵵緔 提交于 2019-12-05 20:32:16
问题 The aim is to find groups of increasing/monotonic numbers given a list of integers. Each item in the resulting group must be of a +1 increment from the previous item Given an input: x = [7, 8, 9, 10, 6, 0, 1, 2, 3, 4, 5] I need to find groups of increasing numbers and achieve: increasing_numbers = [(7,8,9,10), (0,1,2,3,4,5)] And eventually also the number of increasing numbers: len(list(chain(*increasing_numbers))) And also the len of the groups: increasing_num_groups_length = [len(i) for i

Check if two nested lists are equivalent upon substitution

五迷三道 提交于 2019-12-05 19:07:08
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 superpowerset(iterable): s = powerset(iterable) return chain.from_iterable(map(list, combinations(s, r)) for

How to repeat each of a Python list's elements n times with itertools only?

北战南征 提交于 2019-12-05 18:57:16
I have a list with numbers: numbers = [1, 2, 3, 4] . I would like to have a list where they repeat n times like so (for n = 3 ): [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4] . The problem is that I would like to only use itertools for this, since I am very constrained in performance. I tried to use this expression: list(itertools.chain.from_iterable(itertools.repeat(numbers, 3))) But it gives me this kind of result: [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4] which is obviously not what I need. Is there a way to do this with itertools only, without using sorting, loops and list comprehensions? The closest I