1

I'm learning python+chatbot from this site.

I came across this python syntax I'm not sure what it does.

words = [stemmer.stem(w.lower()) for w in words if w != "?"]

Could you please let me know, especially the bracket+expression+space+expression+bracket? words = [stemmer.stem(w.lower()) for w in words if w != "?"]

I tried these in the python interpreter but still can't guess the logic of the syntax:

In [25]: words=[1 for i in [1,2,3] if i !=2]

In [26]: words
Out[26]: [1, 1]

In [27]: words=[2 for i in [1,2,3] if i !=2]

In [28]: words
Out[28]: [2, 2]


In [32]: a=[0 1]

yield invalid syntax (I tried to post the exact message, but the form complains about the format)

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
pg405
  • 65
  • 6
  • That syntax is called List comprehensions https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions – Ryuzaki L Jul 14 '19 at 04:03

0 Answers0