itertools

Generator which leaves a placeholder at the beginning and at the end of the input iterator intact

谁都会走 提交于 2019-12-10 10:06:53
问题 Let's take a list as an example: a = [255, 255, 1, 255, 255, 255, 1, 2, 255, 255, 2, 255, 255, 3, 255, 3, 255, 255, 255] 255 is a special value in it. It's a placeholder. I've made a generator which replaces some of the placeholder inside the list. It works as expected. But I need not to process the beginning placeholders [255, 255 and the ending placeholders 255, 255, 255] and yield them intact. So, I tried to modify the generator to work it out: Python 2.7 from __future__ import print

Pythonic iteration over sliding window pairs in list?

梦想的初衷 提交于 2019-12-10 02:28:44
问题 What's the most Pythonic efficient way to iterate over a list in sliding pairs? Here's a related example: >>> l ['a', 'b', 'c', 'd', 'e', 'f', 'g'] >>> for x, y in itertools.izip(l, l[1::2]): print x, y ... a b b d c f this is iteration in pairs, but how can we get iteration over a sliding pair? Meaning iteration over the pairs: a b b c c d d e etc. which is iteration over the pairs, except sliding the pair by 1 element each time rather than by 2 elements. thanks. 回答1: How about: for x, y in

Python's itertools product memory consumption

泄露秘密 提交于 2019-12-09 14:38:37
问题 The documentation says that the cartesian product function the actual implementation does not build up intermediate results in memory. How can that be possible with generators? Can somebody show me an example with a bounded memory consumption for 2 generators? 回答1: Looking at the module's source code, itertools.product() actually converts every argument to a tuple: // product_new() in itertoolsmodule.c for (i=0; i < nargs ; ++i) { PyObject *item = PyTuple_GET_ITEM(args, i); PyObject *pool =

Cartesian product giving a dictionary

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-09 03:44:34
问题 I have the following lists: brand=["Audi","Mercedes"] speed=[130,150] model=["sport","family"] I want to obtain the equivalent of: ll=[] ll.append({'brand':'mercedes', 'speed':130, 'model':'family'}) ll.append({'brand':'mercedes', 'speed':130, 'model':'sport'}) ll.append({'brand':'audi', 'speed':130, 'model':'family'}) ll.append({'brand':'audi', 'speed':130, 'model':'sport'}) ll.append({'brand':'mercedes', 'speed':150, 'model':'family'}) ll.append({'brand':'mercedes', 'speed':150, 'model':

Python - get all combinations of a list

自古美人都是妖i 提交于 2019-12-09 02:43:53
问题 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? 回答1: Use itertools.combinations and a simple loop to get combinations of all size. combinations return an iterator so you've to pass it to

Iterate over two list of different sizes in python

可紊 提交于 2019-12-08 21:49:34
Value = [1,2,3,4,5,6] content = ['a','b','c','d'] for a,b in itertools.zip_longest(Value , content): print(a,b) The Output that i get using the above code is as follows: 1 a 2 b 3 c 4 d 5 None 6 None what I am Looking for is : 1 a 2 b 3 c 4 d 5 a 6 b basically once one list is exhausted it should take the values again from starting. if any one could help would mean alot You can use itertools.cycle with zip instead: import itertools Value = [1,2,3,4,5,6] content = ['a','b','c','d'] for a,b in zip(Value , itertools.cycle(content)): print(a,b) This outputs: 1 a 2 b 3 c 4 d 5 a 6 b 来源: https:/

Pandas find all combinations of rows under a budget

假装没事ソ 提交于 2019-12-08 11:35:23
问题 I am trying to figure out a way to determine all possible combinations of rows within a DataFrame that are below a budget, so let's say I have a dataframe like this: data = [['Bread', 9, 'Food'], ['Shoes', 20, 'Clothes'], ['Shirt', 15, 'Clothes'], ['Milk', 5, 'Drink'], ['Cereal', 8, 'Food'], ['Chips', 10, 'Food'], ['Beer', 15, 'Drink'], ['Popcorn', 3, 'Food'], ['Ice Cream', 6, 'Food'], ['Soda', 4, 'Drink']] df = pd.DataFrame(data, columns = ['Item', 'Price', 'Type']) df Data Item Price Type

Outputting all possible permutations of multiple lists

微笑、不失礼 提交于 2019-12-08 08:59:12
问题 I am very new to Python and have only just bought my first "Crashcourse in Python" book - originally my choice of language was PHP. My Objective: I desire a script that will output on-screen a list of all possible permutations of a particular pattern. Order is unimportant. The raw data and pattern (the dataset will not change): List1 = ['CA', 'CB', 'CC', 'CD', 'CE', 'CF', 'CG', 'CH', 'CJ', 'CK', 'CL', 'CM', 'CN', 'CO', 'CP', 'CR', 'CS', 'CT', 'CU', 'CV', 'CW', 'CX', 'CY'] List2 = ['51', '02',

iterating over a single list in parallel in python

为君一笑 提交于 2019-12-08 06:44:28
问题 The objective is to do calculations on a single iter in parallel using builtin sum & map functions concurrently . Maybe using (something like) itertools instead of classic for loops to analyze (LARGE) data that arrives via an iterator ... In one simple example case I want to calculate ilen, sum_x & sum_x_sq : ilen,sum_x,sum_x_sq=iterlen(iter),sum(iter),sum(map(lambda x:x*x, iter)) But without converting the (large) iter to a list (as with iter=list(iter) ) n.b. Do this using sum & map and

Iterate over two list of different sizes in python

被刻印的时光 ゝ 提交于 2019-12-08 03:46:57
问题 Value = [1,2,3,4,5,6] content = ['a','b','c','d'] for a,b in itertools.zip_longest(Value , content): print(a,b) The Output that i get using the above code is as follows: 1 a 2 b 3 c 4 d 5 None 6 None what I am Looking for is : 1 a 2 b 3 c 4 d 5 a 6 b basically once one list is exhausted it should take the values again from starting. if any one could help would mean alot 回答1: You can use itertools.cycle with zip instead: import itertools Value = [1,2,3,4,5,6] content = ['a','b','c','d'] for a