Finding elements not in a list

前端 未结 10 796
一向
一向 2020-11-27 13:29

So heres my code:

item = [0,1,2,3,4,5,6,7,8,9]
z = []  # list of integers

for item in z:
    if item not in z:
        print item

z<

10条回答
  •  半阙折子戏
    2020-11-27 14:17

    Your code is a no-op. By the definition of the loop, "item" has to be in Z. A "For ... in" loop in Python means "Loop though the list called 'z', each time you loop, give me the next item in the list, and call it 'item'"

    http://docs.python.org/tutorial/controlflow.html#for-statements

    I think your confusion arises from the fact that you're using the variable name "item" twice, to mean two different things.

提交回复
热议问题