Hi,
I am wondering how this is evaluated.
a=(x for x in [1,2,3,4])
p=[4,5]
c=[x for x in p if x in list(a)]
c is []
but if I expand a first, like a = list(a)
c is [4]
So it seems that the "if" part don't get expanded ? 3 1419 bo****@gmail.com wrote: Hi,
I am wondering how this is evaluated.
a=(x for x in [1,2,3,4]) p=[4,5]
c=[x for x in p if x in list(a)]
c is []
but if I expand a first, like a = list(a)
c is [4]
So it seems that the "if" part don't get expanded ?
Well, for every element in p it recalculates list(a). Since the
generator is exhausted after making a list from it, it gives you
nothing afterwards. So after it checks the first element, it's
equivalent to [x for x in p if x in []]. bo****@gmail.com wrote: Hi,
I am wondering how this is evaluated.
a=(x for x in [1,2,3,4]) p=[4,5]
c=[x for x in p if x in list(a)]
c is []
No it isn't.
In [1]: a=(x for x in [1,2,3,4])
In [2]: p=[4,5]
In [3]: c=[x for x in p if x in list(a)]
In [4]: c
Out[4]: [4]
I'm willing to bet that you used up the 'a' iterator before you ran that
list comprehension, though.
In [5]: c=[x for x in p if x in list(a)]
In [6]: c
Out[6]: []
Note that "x in list(a)" gets executed on each iteration, but the
iterator is used up on the first time.
In [7]: a=(x for x in [1,2,3,4])
In [8]: p = [4, 5, 2, 3]
In [9]: c=[x for x in p if x in list(a)]
In [10]: c
Out[10]: [4]
--
Robert Kern rk***@ucsd.edu
"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
Ah, no wonder. I test with p=[5,4].
thanks. so basically, I still need to expand it first given this
behaviour.
Robert Kern wrote: bo****@gmail.com wrote: Hi,
I am wondering how this is evaluated.
a=(x for x in [1,2,3,4]) p=[4,5]
c=[x for x in p if x in list(a)]
c is []
No it isn't.
In [1]: a=(x for x in [1,2,3,4])
In [2]: p=[4,5]
In [3]: c=[x for x in p if x in list(a)]
In [4]: c Out[4]: [4]
I'm willing to bet that you used up the 'a' iterator before you ran that list comprehension, though.
In [5]: c=[x for x in p if x in list(a)]
In [6]: c Out[6]: []
Note that "x in list(a)" gets executed on each iteration, but the iterator is used up on the first time.
In [7]: a=(x for x in [1,2,3,4])
In [8]: p = [4, 5, 2, 3]
In [9]: c=[x for x in p if x in list(a)]
In [10]: c Out[10]: [4]
-- Robert Kern rk***@ucsd.edu
"In the fields of hell where the grass grows high Are the graves of dreams allowed to die." -- Richard Harter This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Mahesh Padmanabhan |
last post by:
Hi,
When list comprehension was added to the language, I had a lot of
trouble understanding it but now that I am familiar with it, I am not
sure how I programmed in Python without it.
Now I...
|
by: It's me |
last post by:
Okay, I give up.
What's the best way to count number of items in a list?
For instance,
a=,4,5,]
I want to know how many items are there in a (answer should be 7 - I don't
want it to be 4)
|
by: Alan McIntyre |
last post by:
Hi all,
I have a list of items that has contiguous repetitions of values, but
the number and location of the repetitions is not important, so I just
need to strip them out. For example, if my...
|
by: snacktime |
last post by:
I need to convert a generator expression to a list expression so it
will work under python 2.3.
I rewrote this:
for c in range(128):
even_odd = (sum(bool(c & 1<<b) for b in range(8))) & 1
...
|
by: Mike Meyer |
last post by:
Ok, we've added list comprehensions to the language, and seen that
they were good. We've added generator expressions to the language, and
seen that they were good as well.
I'm left a bit...
|
by: Steven Bethard |
last post by:
George Sakkis wrote:
> "Steven Bethard" <steven.bethard@gmail.com> wrote:
>> Dict comprehensions were recently rejected:
>> http://www.python.org/peps/pep-0274.html
>> The reason, of course,...
|
by: Heiko Wundram |
last post by:
Hi all!
The following PEP tries to make the case for a slight unification of for
statement and list comprehension syntax.
Comments appreciated, including on the sample implementation.
===...
|
by: bullockbefriending bard |
last post by:
Given:
class Z(object):
various defs, etc.
class ZList(list):
various defs, etc.
i would like to be able to replace
|
by: bukzor |
last post by:
I have this function:
def write_err(obj):
from sys import stderr
stderr.write(str(obj)+"\n")
and I'd like to rewrite it to take a variable number of objects.
Something like this:
def...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |