简体   繁体   中英

python list comprehension function syntax error

I'm trying to do python list comprehension , but I get a syntax error. I'm not familiar with python list comprehension , so I can't seem to understand why this is erroring out.

>>> s = set()
>>> def in_set(s, val):
        if val in s:
            return True
        else:
            s.add(val)
            return False

>>> [x for x in v0.outV() if x.eid not in_set(s, x.eid)]
  File "<input>", line 1
    [x for x in v0.outV() if x.eid not in_set(s, x.eid)]
                                            ^
SyntaxError: invalid syntax

>>> print "This Works"
This Works
>>> in_set(s, v0.eid)
False

v0.outV() is a generator , x is an object , and x.eid is a string .

Judging by your code structure, I believe that you want this:

[x for x in v0.outV() if not in_set(s, x.eid)]

Here is a reference on list comprehensions .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM