简体   繁体   中英

Am I interpreting this list comprehension correctly in python?

I am still fairly new to python and still have some fumbles when reading list comprehensions. I tried translating two list comprehensions I saw in a tutorial into its elongated form. Did I translate correctly?

list comprehension 1

mytokens = [ word.lemma_.lower().strip() if word.lemma_ != "-PRON-" else word.lower_ for word in mytokens ]

translation 1

for word in mytokens:
    if word.lemma_ != "-PRON-":
        word.lemma_.lower().strip()
    else:
        word.lower_

list comprehension 2

mytokens = [ word for word in mytokens if word not in stopwords and word not in punctuations ]

translation 2

for word in mytokens:
    if word not in stopwords and not in punctuations:
        yield word

for translation 2, I dont think "yield word" would be correct since its not a definition. I am guessing list comprehension 2 does the if statement and places the word back into the list mytokens so maybe it should be a .append?

I think it is right. You are looping correctly. However, you aren't adding the words to a list? Do you mean to be doing this?

So for the first one you could use

my_list = []
for word in mytokens:
    if word.lemma_ != "-PRON-":
        my_list.append(word.lemma_.lower().strip())
    else:
        my_list.append(word.lower_)

By adding them to a list like this it means you can directly compare the output of your translation and the output of the list comprehension. mytokens should be exactly the same as my_list if done correctly.

Also there is a small mistake in the second translation. It should be:

for word in mytokens:
    if word not in stopwords and word not in punctuations:
        yield word

You could also modify this second translation to add all your words to a list.

Let's simplify this:

coll = ["Gerry", "Mary", "Sue"]

comprehended = [ word.lower() for word in coll]

def comprehender(coll):
  coll_out = []
  for word in coll:
      coll_out.append(word.lower())
  return coll_out

If you run this, you can be assured that the two are equivalent by using assert or just return ing comprehended == comprehender(coll)

This is a valid sanity check you can do on any list comprehension, you just vary this pattern to match the logic of your comprehension.

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