您好,在做python基础教程中 ‘历嵌套有字典的列表’练习 时遇到了一个问题:
我希望通过两个for遍历列表,但是为什么运行结果会重复四次呢?
原代码:
alanlaye = {'f_n':'alan','l_n':'laye','age':19,'city':'tokyo'}
izakkull = {'f_n':'izakku','l_n':'ll','age':17,'city':'beijing'}
alexhers = {'f_n':'alex','l_n':'hers','age':21,'city':'marchtown'}
people = [alanlaye,izakkull,alexhers]
for peo in people:
for k,v in peo.items():
print('Full name = '+peo['f_n']+person['l_n'])
print('Age = '+str(peo['age']))
print('City = '+peo['city'])
运行结果及报错内容:
Full name = alanlaye
Age = 19
City = tokyo
Full name = alanlaye
Age = 19
City = tokyo
Full name = alanlaye
Age = 19
City = tokyo
Full name = alanlaye
Age = 19
City = tokyo
Full name = izakkull
Age = 17
City = beijing
Full name = izakkull
Age = 17
City = beijing
Full name = izakkull
Age = 17
City = beijing
Full name = izakkull
Age = 17
City = beijing
Full name = alexhers
Age = 21
City = marchtown
Full name = alexhers
Age = 21
City = marchtown
Full name = alexhers
Age = 21
City = marchtown
Full name = alexhers
Age = 21
City = marchtown
我想要达到的结果:
每一项只出现一次就够了