嗨我有两个列表剪辑和短语。我尝试以两种不同的方式在for循环中运行它们。我期待他们提供相同的输出,但他们给出不同的。怎么样?
snip = ['Hi john', 'Hi sam', 'Hi lila']
phras = ['lets play','lets paint']
for s in snip,phras:
result = s[:]
print result # output is ['lets play', 'lets paint']
# Now lets run again
snip = ['Hi john', 'Hi sam', 'Hi lila']
phras = ['lets play','lets paint']
for s in snip,phras:
result = s[:]
print result
#output is ['Hi john', 'Hi sam', 'Hi lila']
# ['lets play','lets paint']
答案 0 :(得分:5)
您的print语句不在第一个循环中,因此它只显示分配给result
的最后一个值,而第二个print语句位于循环内部,因此打印每个值{ {1}}