generator-expression Questions

4

Solved

Generator expressions is an extremely useful tool, and has a huge advantage over list comprehensions, which is the fact that it does not allocate memory for a new array. The problem I am facing wi...
Product asked 23/3, 2018 at 10:24

13

Solved

When should you use generator expressions and when should you use list comprehensions in Python? # Generator expression (x*2 for x in range(256)) # List comprehension [x*2 for x in range(256)]
Gamma asked 6/9, 2008 at 20:7

3

Solved

I was just messing around in the Python interpreter and I came across some unexpected behavior. >>> bools = (True, True, True, False) >>> all(bools) False >>> any(bools)...
Read asked 15/7, 2019 at 18:26

3

Solved

I coded an algorithm and it worked properly till 2 weeks ago. I get this warning and I cannot understand why I get it. The warning is: "C:/Users/Administrator/Documents/Python/sezg_1_diffne.py:1...

1

Solved

According to this answer lists perform better than generators in a number of cases, for example when used together with str.join (since the algorithm needs to pass over the data twice). In the fol...

8

Solved

I was running a piece of code that unexpectedly gave a logic error at one part of the program. When investigating the section, I created a test file to test the set of statements being run an...
Excreta asked 17/1, 2019 at 23:11

4

Solved

In Python 3, is a list comprehension simply syntactic sugar for a generator expression fed into the list function? e.g. is the following code: squares = [x**2 for x in range(1000)] actually con...

2

Solved

I'm using Python 3.3.1 64-bit on Windows and this code snippet: len ([None for n in range (1, 1000000) if n%3 == 1]) executes in 136ms, compared to this one: sum (1 for n in range (1, 1000000)...
Joinville asked 30/4, 2013 at 19:14

3

Solved

There are, as far as I know, three ways to create a generator through a comprehension1. The classical one: def f1(): g = (i for i in range(10)) The yield variant: def f2(): g = [(yield i) for i ...
Damper asked 19/7, 2017 at 12:32

1

Solved

I just read the question Why is there no tuple comprehension in Python? In the comments of the accepted answer, it is stated that there are no true "tuple comprehensions". Instead, our curr...

1

Solved

Here is an example >>> from timeit import timeit >>> print(timeit('[y for y in range(100)]', number=100000)) 0.7025867114395824 >>> print(timeit('(y for y in range(100))...

3

Solved

When I write: lines = (line.strip() for line in open('a_file')) Is the file opened immediately or is the file system only accessed when I start to consume the generator expression?

1

Solved

Consider the following function, whose output is supposed to be the cartesian product of a sequence of iterables: def cart(*iterables): out = ((e,) for e in iterables[0]) for iterable in iterabl...
Footpad asked 13/7, 2017 at 10:20

1

Solved

I have a generator function which I want to call from another function and return the generator obtained. I can see two approaches here - Note that the below functions are simple dummy functions ...
Panoply asked 12/7, 2017 at 2:21

5

I have a list of dictionaries like the following: lst = [{'a': 5}, {'b': 6}, {'c': 7}, {'d': 8}] I wrote a generator expression like: next((itm for itm in lst if itm['a']==5)) Now the strange...
Mantua asked 5/7, 2017 at 7:50

5

Solved

In the python docs page for any, the equivalent code for the any() function is given as: def any(iterable): for element in iterable: if element: return True return False How does this ...
Monoclinous asked 12/5, 2013 at 8:18

7

Solved

I see this kind of thing sometimes: (k for k in (j for j in (i for i in xrange(10)))) Now this really bends my brain, and I would rather it wasn't presented in this way. Are there any use-cases...
Grillparzer asked 15/3, 2009 at 22:22

1

Solved

Can anyone explain why passing a generator as the only positional argument to a function seems to have special rules? If we have: def f(*args): print "Success!" print args This works,...
Exclamatory asked 11/9, 2015 at 10:21

1

Solved

I have been operating under the theory that generator expressions tend to be more efficient than normal loops. But then I ran into the following example: write a function which given a number, N, a...

1

Solved

The following behaviour seems rather counterintuitive to me (Python 3.4): >>> [(yield i) for i in range(3)] <generator object <listcomp> at 0x0245C148> >>> list([(yie...

6

Solved

I have a list and a lambda function defined as In [1]: i = lambda x: a[x] In [2]: alist = [(1, 2), (3, 4)] Then I try two different methods to calculate a simple sum First method. In [3]: [i(0...

2

Solved

I ran down a bug today that came about because I was using next() to extract a value, and 'not found' emits a StopIteration. Normally that would halt the program, but the function using next was b...
Xylophone asked 2/2, 2015 at 23:39

8

Solved

I have came across this problem a few times and can't seem to figure out a simple solution. Say I have a string string = "a=0 b=1 c=3" I want to convert that into a dictionary with a, b and c be...

1

Solved

I have the following code: import itertools for c in ((yield from bin(n)[2:]) for n in range(10)): print(c) The output is: 0 None 1 None 1 0 None 1 1 None ... etc. Why do the None...
Thulium asked 17/11, 2014 at 18:54

2

Solved

Suppose I have a list a = [0, 1, 2]. I know that I can use a list comprehension like so, to get a list where the values are doubled: >>> b = [x*2 for x in a] >>> b [0, 2, 4] How ...
Crossindex asked 26/6, 2014 at 23:59

© 2022 - 2024 — McMap. All rights reserved.