Nested list nesting order and nested generator expression in python

I'm new to Python and confused about some of the code in the official Python documentation.

unique_words = set(word  for line in page  for word in line.split())

      

To me it looks equivalent to:

unique_words=set()
for word in line.split():
    for line in page:
        unique_words.add(word)

      

How can a string be used in the first loop before it is defined in the nested loop? However, it does work. I think this assumes a left-to-right nesting order of the list and generator expression, which is contrary to my previous understanding.

Can anyone clarify the correct order for me?

+3


source to share


5 answers


word for line in page for word in line.split()

this part works like this: -

for line in page:
    for word in line.split():
        print word

      



()

this makes it a "generator function so the general job of the operator is as follows: -

def solve():
    for line in page:
        for word in line.split():
            yield word

      

and set () is used to avoid duplicating or repeating the same word as the code intended to get "unique words".

+4


source


From the tutorial on the official documentation:

A list comprehension consists of parentheses containing an expression, followed by a for clause, and then zero or more for clauses or for clauses. The result is a new list, resulting from evaluating the expression in the context of the following for and if clauses. For example, this listcomp concatenates the elements of two lists if they are not equal:
>>> [(x, y) for x in [1,2,3] for y in [3,1,4] if x! = y]
[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
and its equivalent:
>>> combs = []
>>> for x in [1,2,3]:
... for y in [3,1,4]:
... if x! = y:
... combs.append ((x, y))
...
>>> combs
[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
Note that the order of the for and if statements is the same in both of these snippets.

See the last sentence above.



Also note that the construct you are describing is not (officially) called "nested list comprehension". A nested list comprehension implies a list comprehension that is within another list comprehension, for example (again from the tutorial):

[[row[i] for row in matrix] for i in range(4)]

      

What you are asking is just a list comprehension with a few sentences for

.

+1


source


Your loops are wrong. Use this:

unique_words = set(word for line in page for word in line.split())
print unique_words

l = []
for line in page:
    for word in line.split():
        l.append(word)
print set(l)

      

output:

C:\...>python test.py
set(['sdaf', 'sadfa', 'sfsf', 'fsdf', 'fa', 'sdf', 'asd', 'asdf'])
set(['sdaf', 'sadfa', 'sfsf', 'fsdf', 'fa', 'sdf', 'asd', 'asdf'])

      

0


source


You have mixed nested loops. What the code does:

unique_words={}
for line in page:
    for word in line.split():
        unique_words.add(word)

      

0


source


In addition to the correct answers that emphasized the order point, I would add that we are using set to remove duplicates from the string to make "unique words". check this and this thread

unique_words = set(word for line in page for word in line.split())
print unique_words

l = {}
for line in page:
    for word in line.split():
        l.add(word)
print l

      

0


source







All Articles