a = [1, 2, 3]
b = (c for c in a if c in a)
a = [2, 3, 4]
print(list(b))
def d(expr):
for c in expr:
if c in expr:
yield c
a1 = [1,2,3]
t = d(a1)
a1 = [2,3,4]
print(list(t))
conclusion:
[2, 3]
[1, 2, 3]
Question: 1) in the first version, the loop keeps the old value of the list ([1,2,3]), but the conditional expression takes the new list a ([2,3,4]). 2) how does my own generator give me a different result than the generative expression?
For example, I replaced a on the real lists of the first example, I hope this becomes more clear my first question.
a = [1, 2, 3]
b = (c for c in [1,2,3] if c in [2, 3, 4])
a = [2, 3, 4]
print(list(b))
Conclusion:
[2,3]
source
share