generator-expression

How to return a generator from another function

梦想的初衷 提交于 2019-12-10 14:57:16
问题 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 to illustrate the purpose. Please don't come up with better ways to implement those functions itself. Method 1 def fun_a(n): for i in range(n): yield i+10 def fun_b(n): if n < 0: yield None return yield fun_a(n) and use it as list(list(fun_b(10))[0]) to get [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

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

末鹿安然 提交于 2019-12-08 04:26:00
问题 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

Django Custom Queryset filters

荒凉一梦 提交于 2019-12-04 11:28:35
问题 Is there, in Django, a standard way to write complex, custom filters for QuerySets? Just as I can write MyClass.objects.all().filter(field=val) I'd like to do something like this : MyClass.objects.all().filter(customFilter) I could use a generator expression (x for x in MyClass.objects.all() if customFilter(x)) but that would lose the chainability and whatever other functions the QuerySets provide. 回答1: I think you may need custom managers. 回答2: The recommendation to start using manager

Differences between generator comprehension expressions

核能气质少年 提交于 2019-12-04 09:07:34
问题 There are, as far as I know, three ways to create a generator through a comprehension 1 . The classical one: def f1(): g = (i for i in range(10)) The yield variant: def f2(): g = [(yield i) for i in range(10)] The yield from variant (that raises a SyntaxError except inside of a function): def f3(): g = [(yield from range(10))] The three variants lead to different bytecode, which is not really surprising. It would seem logical that the first one is the best, since it's a dedicated,

“Tuple comprehensions” and the star splat/unpack operator *

五迷三道 提交于 2019-12-03 17:31:32
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 current option is to use a generator expression and pass the resulting generator object to the tuple constructor: tuple(thing for thing in things) Alternatively, we can create a list using a list comprehension and then pass the list to the tuple constructor: tuple([thing for thing in things]) Lastly and to the contrary of the accepted answer, a more recent answer stated that tuple comprehensions are indeed a

convert string to dict using list comprehension

强颜欢笑 提交于 2019-12-03 11:33:51
问题 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 being the key and 0, 1, and 3 being their respective values (converted to int). Obviously I can do this: list = string.split() dic = {} for entry in list: key, val = entry.split('=') dic[key] = int(val) But I don't really like that for loop, It seems so simple that you should be able to convert it to

convert string to dict using list comprehension

情到浓时终转凉″ 提交于 2019-12-03 01:59:46
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 being the key and 0, 1, and 3 being their respective values (converted to int). Obviously I can do this: list = string.split() dic = {} for entry in list: key, val = entry.split('=') dic[key] = int(val) But I don't really like that for loop, It seems so simple that you should be able to convert it to some sort of list comprehension expression. And that works for slightly simpler cases where the val can be

Differences between generator comprehension expressions

旧街凉风 提交于 2019-12-03 01:51:06
There are, as far as I know, three ways to create a generator through a comprehension 1 . The classical one: def f1(): g = (i for i in range(10)) The yield variant: def f2(): g = [(yield i) for i in range(10)] The yield from variant (that raises a SyntaxError except inside of a function): def f3(): g = [(yield from range(10))] The three variants lead to different bytecode, which is not really surprising. It would seem logical that the first one is the best, since it's a dedicated, straightforward syntax to create a generator through comprehension. However, it is not the one that produces the

Why this list comprehension is faster than equivalent generator expression?

吃可爱长大的小学妹 提交于 2019-12-01 05:58:47
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) if n%3 == 1) which executes in 146ms. Shouldn't a generator expression be faster or the same speed as the list comprehension in this case? I quote from Guido van Rossum From List Comprehensions to Generator Expressions : ...both list comprehensions and generator expressions in Python 3 are actually faster than they were in Python 2! (And there is no longer a speed difference between the two.) EDIT: I

Why this list comprehension is faster than equivalent generator expression?

…衆ロ難τιáo~ 提交于 2019-12-01 03:52:46
问题 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) if n%3 == 1) which executes in 146ms. Shouldn't a generator expression be faster or the same speed as the list comprehension in this case? I quote from Guido van Rossum From List Comprehensions to Generator Expressions: ...both list comprehensions and generator expressions in Python 3 are actually