列表打印结果有些异常

时间:2016-07-29 14:35:48

标签: python python-2.7

嗨我有两个列表剪辑和短语。我尝试以两种不同的方式在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']

1 个答案:

答案 0 :(得分:5)

您的print语句不在第一个循环中,因此它只显示分配给result的最后一个值,而第二个print语句位于循环内部,因此打印每个值{ {1}}