itertools

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

纵然是瞬间 提交于 2019-12-18 18:59:30
问题 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

Failing to import itertools in Python 3.5.2

微笑、不失礼 提交于 2019-12-18 15:08:09
问题 I am new to Python. I am trying to import izip_longest from itertools. But I am not able to find the import "itertools" in the preferences in Python interpreter. I am using Python 3.5.2. It gives me the below error- from itertools import izip_longest ImportError: cannot import name 'izip_longest' Please let me know what is the right course of action. I have tried Python 2.7 too and ended up with same problem. Do I need to use lower version Python. 回答1: izip_longest was renamed to zip_longest

Python permutations with constraints

强颜欢笑 提交于 2019-12-18 11:13:12
问题 I am using python 3 and I am trying to find a way to get all the permutations of a list while enforcing some constraints. For instance, I have a list L=[1, 2, 3, 4, 5, 6, 7] I want to find all permutations. However, My constraints are: 1 should always come before 2. 3 should come before 4 which in turn should come before 5. Finally, 6 should come before 7. Of course, I can generate all permutations and ignore those which do not follow these constraints but this wouldn't be efficient I guess.

Determine all combinations of flipping a coin without using “itertools.product”

ぐ巨炮叔叔 提交于 2019-12-18 09:53:46
问题 I went through similar posts on the forum but all of them suggest using itertools.product but I was wondering if it can be solved without using it. I want to print all the combinations of outcomes for N flips of a coin. This can be done if N is known in advance. So the number of nested loops will be just N. But if N has to be determined dynamically ( input() function) then I am stuck in implementing it in code. In plain English it is easy to imagine that the number of for loops is

Itertools product without repeating duplicates

孤街醉人 提交于 2019-12-18 07:43:36
问题 from itertools import product teams = ['india', 'australia', 'new zealand'] word_and = ['and'] tmp = '%s %s %s' items = [teams, word_and, teams] print(list(tmp % a for a in list(product(*items)))) prints: ['india and india', 'india and australia', 'india and new zealand', 'australia and india', 'australia and australia', 'australia and new zealand', 'new zealand and india', 'new zealand and australia', 'new zealand and new zealand'] How to: avoid the same name repeating in a single sentence

Faster numpy-solution instead of itertools.combinations?

♀尐吖头ヾ 提交于 2019-12-18 04:55:15
问题 I'm using itertools.combinations() as follows: import itertools import numpy as np L = [1,2,3,4,5] N = 3 output = np.array([a for a in itertools.combinations(L,N)]).T Which yields me the output I need: array([[1, 1, 1, 1, 1, 1, 2, 2, 2, 3], [2, 2, 2, 3, 3, 4, 3, 3, 4, 4], [3, 4, 5, 4, 5, 5, 4, 5, 5, 5]]) I'm using this expression repeatedly and excessively in a multiprocessing environment and I need it to be as fast as possible. From this post I understand that itertools -based code isn't the

Weirdness of itertools.groupby in Python when realizing the groupby result early [duplicate]

对着背影说爱祢 提交于 2019-12-17 21:24:08
问题 This question already has answers here : python groupby behaviour? (3 answers) Closed 4 years ago . First, apologies for my poor description of the problem. I can't find a better one. I found that applying list to an itertools.groupby result will destroy the result. See code: import itertools import operator log = '''\ hello world hello there hi guys hi girls'''.split('\n') data = [line.split() for line in log] grouped = list(itertools.groupby(data, operator.itemgetter(0))) for key, group in

Does itertools.product evaluate its arguments lazily?

≯℡__Kan透↙ 提交于 2019-12-17 20:47:25
问题 The following never prints anything in Python 3.6 from itertools import product, count for f in product(count(), [1,2]): print(f) Instead, it just sits there and burns CPU. The issue seems to be that product never returns an iterator if it's over an infinite space because it evaluates the full product first. This is surprising given that the product is supposed to be a generator. I would have expected this to start counting up (to infinity), something like the behavior of this generator

python return lists of continuous integers from list

半世苍凉 提交于 2019-12-17 19:00:01
问题 I have a list of integers, and I want to generate a list containing a list of all the continuous integers. #I have: full_list = [0,1,2,3,10,11,12,59] #I want: continuous_integers = [[0,1,2,3], [10,11,12], [59]] I have the following which works, but seems like a poor way to do it: sub_list = [] continuous_list = [] for x in full_list: if sub_list == []: sub_list.append(x) elif x-1 in sub_list: sub_list.append(x) else: continuous_list.append(sub_list) sub_list = [x] continuous_list.append(sub

Python merging two lists with all possible permutations

為{幸葍}努か 提交于 2019-12-17 10:33:22
问题 I'm trying to figure out the best way to merge two lists into all possible combinations. So, if I start with two lists like this: list1 = [1, 2] list2 = [3, 4] The resulting list will look like this: [[[1,3], [2,4]], [[1,4], [2,3]]] That is, it basically produces a list of lists, with all the potential combinations between the two. I've been working through itertools, which I'm pretty sure holds the answer, but I can't come up with a way to make it act this way. The closest I came was: list1