itertools

Can someone please explain this error - “Runtime error: dictionary size changed during iteration”? [closed]

ⅰ亾dé卋堺 提交于 2019-12-25 01:55:57
问题 This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 6 years ago . def find_the_best_solution(shipping_request, list, current_solution, best_shipping_solution): if shipping_request == {}: return current_solution for item

Counting “unique pairs” of numbers into a python dictionary?

早过忘川 提交于 2019-12-24 19:34:13
问题 EDIT: Edited typos; the key values of the dictionary should be dictionaries, not sets. I will keep the typos here though, as the questions below address this question. My apologies for the confusion. Here's the problem: Let's say I have a list of integers whereby are never repeats: list1 = [2, 3] In this case, there is a unique pair 2-3 and 3-2, so the dictionary should be: {2:{3: 1}, 3:{2: 1}} That is, there is 1 pair of 2-3 and 1 pair of 3-2. For larger lists, the pairing is the same, e.g.

MemoryError while trying to using itertools.permutations, how use less memory?

*爱你&永不变心* 提交于 2019-12-24 16:28:33
问题 I'm loading from a text document containing so random strings and I'm trying to print every possible permutation of the characters in that string. If the notepad contains for example: 123 abc I want my output to be 123,132,213,231,312,321 abc,acb,bac,bca,cab,cba The text file contains some pretty large strings so I can see why I am getting this MemoryError. My first attempt I used this: import sys import itertools import math def organize(to_print): number_list = [] upper_list = [] lower_list

How do I convert Python's itertools.product library from a list comprehension to normal for loops?

痞子三分冷 提交于 2019-12-24 15:12:18
问题 According to http://docs.python.org/2/library/itertools.html#itertools.product the following function is equivalent to using their library (I removed a few things I don't need from it): def product(*args): # product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy pools = map(tuple, args) result = [[]] for pool in pools: result = [x+[y] for x in result for y in pool] for prod in result: yield tuple(prod) In my case I'm passing the product function 3 lists, but I need to add some conditional checks,

Python generating all nondecreasing sequences

给你一囗甜甜゛ 提交于 2019-12-24 13:27:30
问题 I am having trouble finding a way to do this in a Pythonic way. I assume I can use itertools somehow because I've done something similar before but can't remember what I did. I am trying to generate all non-decreasing lists of length L where each element can take on a value between 1 and N. For example if L=3 and N=3 then [1,1,1],[1,1,2],[1,1,3],[1,2,2],[1,2,3], etc. 回答1: You can do this using itertools.combinations_with_replacement: >>> L, N = 3,3 >>> cc = combinations_with_replacement(range

How to calculate the number of all possible combinations for a range of numbers from 1 to N?

僤鯓⒐⒋嵵緔 提交于 2019-12-24 11:32:25
问题 Other than doing this: from itertools import combinations def brute_force(x): for l in range (1,len(x)+1): for f in list(combinations(range(0,len(x)),l)): yield f x = range(1,18) len(list(brute_force(x))) [out]: 131071 How could I mathematically calculate the number of all possible combinations? Is there a way to do it computationally without enumerating the possible combinations? 回答1: Always there is 2 n −1 non-empty subset of set {1,...,n} . For example consider the list ['a','b','c'] : >>>

itertools groupby object not outputting correctly

浪子不回头ぞ 提交于 2019-12-24 11:29:34
问题 I was trying to use itertools.groupby to help me group a list of integers by positive or negative property, for example: input [1,2,3, -1,-2,-3, 1,2,3, -1,-2,-3] will return [[1,2,3],[-1,-2,-3],[1,2,3],[-1,-2,-3]] However if I: import itertools nums = [1,2,3, -1,-2,-3, 1,2,3, -1,-2,-3] group_list = list(itertools.groupby(nums, key=lambda x: x>=0)) print(group_list) for k, v in group_list: print(list(v)) >>> [] [-3] [] [] But if I don't list() the groupby object, it will work fine: nums = [1,2

More simplified explanation of chain.from_iterable and chain() of itertools

我只是一个虾纸丫 提交于 2019-12-24 09:37:56
问题 Can you give a more simplified explanation of these two methods chain() and chain.from_iterable from itertools ? I have searched the knowledge base and as well the python documentation but i got confused. I am new to python that's why I am asking a more simplified explanation regarding these. Thanks! 回答1: You can chain sequences to make a single sequence: >>> from itertools import chain >>> a = [1, 2, 3] >>> b = ['a', 'b', 'c'] >>> list(chain(a, b)) [1, 2, 3, 'a', 'b', 'c'] If a and b are in

How to create a list based on same value of a dictionary key

六月ゝ 毕业季﹏ 提交于 2019-12-24 08:59:30
问题 I am trying to join together dictionaries that contain the same date, and also create a list of the temperature values that these common dates have to then pull the max and min of these values. I have this: data = [{'temp_min': 51.75, 'date': '2019-05-31', 'temp_max': 52.25}, {'temp_min': 52.5, 'date': '2019-05-31', 'temp_max': 52.87}, {'temp_min': 53.29, 'date': '2019-05-31', 'temp_max': 53.55}, {'temp_min': 68.19, 'date': '2019-06-01', 'temp_max': 75.19}, {'temp_min': 61.45, 'date': '2019

Python: performance issues with islice

二次信任 提交于 2019-12-24 08:40:37
问题 With the following code, I'm seeing longer and longer execution times as I increase the starting row in islice. For example, a start_row of 4 will execute in 1s but a start_row of 500004 will take 11s. Why does this happen and is there a faster way to do this? I want to be able to iterate over several ranges of rows in a large CSV file (several GB) and make some calculations. import csv import itertools from collections import deque import time my_queue = deque() start_row = 500004 stop_row =