l = []
for i in xrange(10):
l.append({‘num’:i})
print l
在考虑以下代码,运行结束后的结果是什么?
l = []
a = {‘num’:0}
for i in xrange(10):
a[‘num’] = i
l.append(a)
print l
以上两段代码的运行结果是否相同,如果不相同,原因是什么?
上方代码的结果:
1. [{‘num’:0},{‘num’:1},{‘num’:2},{‘num’:3},{‘num’:4},{‘num’:5},{‘num’:6},{‘num’:7},{‘num’:8},
{‘num’:9}]
下方代码结果:
1. [{‘num’:9},{‘num’:9},{‘num’:9},{‘num’:9},{‘num’:9},{‘num’:9},{‘num’:9},{‘num’:9},{‘num’:9},
{‘num’:9}]