generator-expression

Why converting list to set is faster than converting generator to set?

本小妞迷上赌 提交于 2021-02-04 19:47:25
问题 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))', number=100000)) 0.09295392291478244 >>> print(timeit('set([y for y in range(100)])', number=100000)) 1.0864544935180334 >>> print(timeit('set((y for y in range(100)))', number=100000)) 1.1277489876506621 It is very confusing. Generator takes less time to create(and that is understandable) but why converting generator to set

Nested generator expression - unexpected result [duplicate]

爷,独闯天下 提交于 2021-02-04 16:38:05
问题 This question already has answers here : Accessing class variables from a list comprehension in the class definition (5 answers) Why is one class variable not defined in list comprehension but another is? (1 answer) Closed 6 years ago . Here's the test code: units = [1, 2] tens = [10, 20] nums = (a + b for a in units for b in tens) units = [3, 4] tens = [30, 40] [x for x in nums] Under the assumption that the generator expression on line 3 ( nums = ... ) forms an iterator I would expect the

Cmake Generator expressions with multiple entries (spaces?)

瘦欲@ 提交于 2021-01-29 08:30:33
问题 (During writing this question I found a solution, so this just documents it, because I would really have needed it beforehand!) Im writing a c++ project and use cmake for it. In my cmake file I have: set(MY_DEBUG_WARNINGS "-Og -Wall" ) # Add warnings: add_compile_options("$<$<CONFIG:DEBUG>:${MY_DEBUG_WARNINGS}>" "$<$<CXX_COMPILER_ID:Clang,AppleClang>:-fcolor-diagnostics>" "$<$<CXX_COMPILER_ID:GNU>:-fdiagnostics-color>" ) which doesn't work, because the compile command picks up extra escaped

Sum of objects' prices in Django template

自闭症网瘾萝莉.ら 提交于 2021-01-28 08:18:46
问题 I would like to compute the total of a shopping cart in my template: This is my template with a table of products. I tried to use a Generator expression in my cart method but it doesn't work. Any thoughts? cart.html <table> {% if not cart_list %} {{ "The cart is empty" }} {% else %} <tr> <th>Name</th> <th>Price</th> </tr> {% for product in cart_list %} <tr> <td>{{ product.name }}</td> <td>${{ product.price }}</td> </tr> {% endfor %} <tr> <td>{{ Total }}</td> <td>{{ total_prices }}</td> </tr>

Unexpected behaviour with a conditional generator expression [duplicate]

醉酒当歌 提交于 2020-01-11 15:33:56
问题 This question already has answers here : Generator expression uses list assigned after the generator's creation (5 answers) Closed 12 months ago . 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 and found out an unusual bug that seems very odd. I tested this simple code: array = [1, 2, 2, 4, 5] # Original array f = (x for x in array if array.count(x) ==

Why does `yield from` in a generator expression yield `None`s?

∥☆過路亽.° 提交于 2020-01-03 17:45:14
问题 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 s appear? If I instead have: def hmm(): for n in range(10): yield from bin(n)[2:] for c in hmm(): print(c) Then I get what I would expect: 0 1 1 0 1 1 ... etc. Further, is there a way to write it as the generator expression to get the same result as the latter? 回答1: yield is an expression, and its value is whatever

Short-circuiting list comprehensions [duplicate]

不问归期 提交于 2020-01-02 03:16:27
问题 This question already has answers here : Using while in list comprehension or generator expressions (2 answers) Closed 4 years ago . On several occasions I've wanted python syntax for short-circuiting list comprehensions or generator expressions. Here is a simple list comprehension, and the equivalent for loop in python: my_list = [1, 2, 3, 'potato', 4, 5] [x for x in my_list if x != 'potato'] result = [] for element in my_list: if element != 'potato': result.append(element) There isn't

Need to understand Python generator object

久未见 提交于 2019-12-29 07:51:51
问题 In the following: name = 'TODD' chars = set('AEIOU') for ii in range(-1, int(math.copysign(len(name) + 1, -1)), -1): if any((cc in chars) for cc in name[ii]): print 'Found' else: print 'Not Found' I understand that what's inside any(...) is a generator object. What I don't understand is the lack of parentheses - if the parentheses belong to the any() function, shouldn't there be another set of parentheses around the generator expression? Thanks. 回答1: The parenthesis can be omitted when used

Need to understand Python generator object

老子叫甜甜 提交于 2019-12-29 07:51:04
问题 In the following: name = 'TODD' chars = set('AEIOU') for ii in range(-1, int(math.copysign(len(name) + 1, -1)), -1): if any((cc in chars) for cc in name[ii]): print 'Found' else: print 'Not Found' I understand that what's inside any(...) is a generator object. What I don't understand is the lack of parentheses - if the parentheses belong to the any() function, shouldn't there be another set of parentheses around the generator expression? Thanks. 回答1: The parenthesis can be omitted when used

Converting a yield statement to a Generator Expression in Python

跟風遠走 提交于 2019-12-24 03:42:36
问题 I have a question regarding converting a yield statement to a generator expression So I have this small yield method that gets a function and a starting number as its inputs, and basically calls the function for each previous number that was called i.e: The first call returns the initial number The second call returns the function(initial number) The third call returns the function(second number) The fourth call returns the function(third number) etc. Here is the code in Python: def some_func