generator-expression

How to identify a generator vs list comprehension

旧城冷巷雨未停 提交于 2019-12-01 03:06:51
问题 I have this: >>> sum( i*i for i in xrange(5)) My question is, in this case am I passing a list comprehension or a generator object to sum ? How do I tell that? Is there a general rule around this? Also remember sum by itself needs a pair of parentheses to surround its arguments. I'd think that the parentheses above are for sum and not for creating a generator object. Wouldn't you agree? 回答1: You are passing in a generator expression. A list comprehension is specified with square brackets ( [.

Generator as function argument

本小妞迷上赌 提交于 2019-11-28 15:35:48
问题 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, as expected. >>> f(1, *[2]) Success! (1, 2) This does not work, as expected. >>> f(*[2], 1) File "<stdin>", line 1 SyntaxError: only named arguments may follow *expression This works, as expected >>> f(1 for x in [1], *[2]) Success! (generator object <genexpr> at 0x7effe06bdcd0>, 2) This works, but I

Are list comprehensions syntactic sugar for `list(generator expression)` in Python 3?

大城市里の小女人 提交于 2019-11-27 09:23:55
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 converted in the background into the following? squares = list(x**2 for x in range(1000)) I know the output is identical, and Python 3 fixes the surprising side-effects to surrounding namespaces that list comprehensions had, but in terms of what the CPython interpreter does under the hood, is the former converted to the latter, or are there any difference in how the code gets executed? Background I found

Using while in list comprehension or generator expressions

旧巷老猫 提交于 2019-11-27 04:37:34
I can use if and for in list comprehensions/generator expressions as list(i for i in range(100) if i*i < 30) I know this is not the most efficient but bear with me as the condition could be much more complicated and this is just an example. However, this still goes through hundred iterations and only yields a value in the first 6. Is there a way to tell the generator expression where to stop with something like this: list(i for i in range(100) while i*i < 30) However, while is not understood in generator expressions. So, my question is, how do I write a generator expression with a stopping

Why results of map() and list comprehension are different?

╄→尐↘猪︶ㄣ 提交于 2019-11-26 19:02:32
The following test fails: #!/usr/bin/env python def f(*args): """ >>> t = 1, -1 >>> f(*map(lambda i: lambda: i, t)) [1, -1] >>> f(*(lambda: i for i in t)) # -> [-1, -1] [1, -1] >>> f(*[lambda: i for i in t]) # -> [-1, -1] [1, -1] """ alist = [a() for a in args] print(alist) if __name__ == '__main__': import doctest; doctest.testmod() In other words: >>> t = 1, -1 >>> args = [] >>> for i in t: ... args.append(lambda: i) ... >>> map(lambda a: a(), args) [-1, -1] >>> args = [] >>> for i in t: ... args.append((lambda i: lambda: i)(i)) ... >>> map(lambda a: a(), args) [1, -1] >>> args = [] >>> for

yield in list comprehensions and generator expressions

心已入冬 提交于 2019-11-26 17:22:21
The following behaviour seems rather counterintuitive to me (Python 3.4): >>> [(yield i) for i in range(3)] <generator object <listcomp> at 0x0245C148> >>> list([(yield i) for i in range(3)]) [0, 1, 2] >>> list((yield i) for i in range(3)) [0, None, 1, None, 2, None] The intermediate values of the last line are actually not always None , they are whatever we send into the generator, equivalent (I guess) to the following generator: def f(): for i in range(3): yield (yield i) It strikes me as funny that those three lines work at all. The Reference says that yield is only allowed in a function

Are list comprehensions syntactic sugar for `list(generator expression)` in Python 3?

有些话、适合烂在心里 提交于 2019-11-26 14:40:30
问题 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 converted in the background into the following? squares = list(x**2 for x in range(1000)) I know the output is identical, and Python 3 fixes the surprising side-effects to surrounding namespaces that list comprehensions had, but in terms of what the CPython interpreter does under the hood, is the former

Using while in list comprehension or generator expressions

只谈情不闲聊 提交于 2019-11-26 11:17:03
问题 I can use if and for in list comprehensions/generator expressions as list(i for i in range(100) if i*i < 30) I know this is not the most efficient but bear with me as the condition could be much more complicated and this is just an example. However, this still goes through hundred iterations and only yields a value in the first 6. Is there a way to tell the generator expression where to stop with something like this: list(i for i in range(100) while i*i < 30) However, while is not understood

yield in list comprehensions and generator expressions

白昼怎懂夜的黑 提交于 2019-11-26 05:22:26
问题 The following behaviour seems rather counterintuitive to me (Python 3.4): >>> [(yield i) for i in range(3)] <generator object <listcomp> at 0x0245C148> >>> list([(yield i) for i in range(3)]) [0, 1, 2] >>> list((yield i) for i in range(3)) [0, None, 1, None, 2, None] The intermediate values of the last line are actually not always None , they are whatever we send into the generator, equivalent (I guess) to the following generator: def f(): for i in range(3): yield (yield i) It strikes me as

List comprehension vs generator expression&#39;s weird timeit results?

谁说胖子不能爱 提交于 2019-11-26 01:32:02
I was answering this question , I preferred generator expression here and used this, which I thought would be faster as generator doesn't need to create the whole list first: >>> lis=[['a','b','c'],['d','e','f']] >>> 'd' in (y for x in lis for y in x) True And Levon used list comprehension in his solution , >>> lis = [['a','b','c'],['d','e','f']] >>> 'd' in [j for i in mylist for j in i] True But when I did the timeit results for these LC was faster than generator: ~$ python -m timeit -s "lis=[['a','b','c'],['d','e','f']]" "'d' in (y for x in lis for y in x)" 100000 loops, best of 3: 2.36 usec