itertools

itertools product speed up

一世执手 提交于 2019-12-17 04:01:26
问题 I use itertools.product to generate all possible variations of 4 elements of length 13. The 4 and 13 can be arbitrary, but as it is, I get 4^13 results, which is a lot. I need the result as a Numpy array and currently do the following: c = it.product([1,-1,np.complex(0,1), np.complex(0,-1)], repeat=length) sendbuf = np.array(list(c)) With some simple profiling code shoved in between, it looks like the first line is pretty much instantaneous, whereas the conversion to a list and then Numpy

Product method in c#

你离开我真会死。 提交于 2019-12-14 04:14:26
问题 I am working with python, and I am implementing my code to c# and in python there is method "product", anyone knows if there is something similar in c#? if not maybe someone can put me on a track of how to write this function by myself? example of product: a=[[[(1, 2), (3, 4)], [(5, 6), (7, 8)], [(9, 10), (11, 12)]], [[(13, 14), (15, 16)]]] b= product(*a) output: ([(1, 2), (3, 4)], [(13, 14), (15, 16)]) ([(5, 6), (7, 8)], [(13, 14), (15, 16)]) ([(9, 10), (11, 12)], [(13, 14), (15, 16)]) 回答1:

The Concept Behind itertools's product Function

不想你离开。 提交于 2019-12-14 03:33:07
问题 so basically i want to understand the concept of product() function in itertools. i mean what is the different between yield and return. And can this code be shorten down anyway. def product1(*args, **kwds): pools = map(tuple, args) * kwds.get('repeat', 1) n = len(pools) if n == 0: yield () return if any(len(pool) == 0 for pool in pools): return indices = [0] * n yield tuple(pool[i] for pool, i in zip(pools, indices)) while 1: for i in reversed(range(n)): # right to left if indices[i] == len

How to fill a 2D Python numpy array with values from a generator?

非 Y 不嫁゛ 提交于 2019-12-13 17:26:04
问题 Based on the answers here it doesn't seem like there's an easy way to fill a 2D numpy array with data from a generator. However, if someone can think of a way to vectorize or otherwise speed up the following function I would appreciate it. The difference here is that I want to process the values from the generator in batches rather than create the whole array in memory. The only way I could think of doing that was with a for loop. import numpy as np from itertools import permutations

For binomial function nCr=k, given r and k find n

本秂侑毒 提交于 2019-12-13 10:00:30
问题 I need a function that can solve the following: for a binomial function nCr=k, given r and k find n. in mathematics nCr=n!/r!(n-r)! I tried following but it doesn't solve it. for example 8C6=28, for my function the inputs are 6 and 28 and i want to find 8. This may not have exact integer number so I want to find an x>=n. """I am approaching it this way, i.e. find the solution of a polynomial function iteratively, hope there is a better way""" def find_n(r,k): #solve_for_n_in(n*(n-1)...(n-r)

Cartesian Product for two dictionaries python

十年热恋 提交于 2019-12-13 06:06:38
问题 ok so i've got two dictionaries. dictionary_1 = {'status': ['online', 'Away', 'Offline'], 'Absent':['yes', 'no', 'half day']} dictionary_2 = {'healthy': ['yes', 'no'], 'insane': ['yes', 'no'] Now i need to combine them so that i get a new dictionary with: {'status': ['online', 'online', 'away', 'away', 'Offline', 'Offline'], 'Absent': ['yes', 'yes', 'no', 'no', 'half day', 'half day'], 'healthy': ['yes', 'no', 'yes', 'no', 'yes', 'no'], 'insane': ['yes', 'no', 'yes', 'no', 'yes', 'no'] } This

itertools cycle in vigenere cipher causing problems with spaces python

流过昼夜 提交于 2019-12-13 05:20:59
问题 In my code for vigenere cipher I use cycle from itertools to cycle through the key word. This works great until I use spaces in the message as it encrypts the space therefore making the encryption wrong. Here is the code. message = input('enter message: ') keyword = input('enter keyword: ') def chr_to_int(char): return 0 if char == 'z' else ord(char)-96 def int_to_chr(integer): return 'z' if integer == 0 else chr(integer+96) def add_chars(a, b): return int_to_chr(( chr_to_int(a) + chr_to_int

using itertools to generate the Cartesian product of list of lists

萝らか妹 提交于 2019-12-13 05:10:05
问题 I'm trying to solve the problem mentioned in this post. Consider the D=[d1,...,dm] a list of non-negative integers. I want to have the set of the Cartesian products of range(d1),...,range(dm) . For example if m=3 I could use itertools: indices=[i for i in itertools.product(range(d1),range(d2),range(d3))] I would appreciate if you could help me know how I can generate the indices using D with arbitrary length. 回答1: You can use map to map all items of D to range and then unpack them for product

Find all binary strings of certain weight has fast as possible

大憨熊 提交于 2019-12-13 03:54:56
问题 I want to find binary strings of a certain weight. The amount of such strings grows to the point of a memory error, so I'm currently generating them with a generator. This code generators all length n binary strings with weight k: def kbits(n, k): for bits in itertools.combinations(range(n), k): s = ['0'] * n for bit in bits: s[bit] = '1' yield ''.join(s) for b in kbits(length, weight): print(b) So for length = 3 and weight = 2, we get 110, 101, 011. My research requires me to parse through

Nested GROUP BY in django: returning Objects

给你一囗甜甜゛ 提交于 2019-12-13 02:08:22
问题 I'm trying to use a classic database GROUP_BY on my queryset in Django. I've already seen How to query as GROUP BY in django? but the distinction is : that I need to get a dictionnary of objects , not values (indeed not the type of thing returned by values() ). that I need to get a nested dictionary (i.e, I need to GROUP_BY on GROUP_BY). My model: class CitiesTable(models.Model): country = models.TextField() region_or_state = models.TextField() city = models.TextField() I want to classify by