python 1:列表和字典

初学Python, 对列表和字典的嵌套使用.

 1 phoneBook = [] #列表 list

 2 peopleInfo = {} #字典 dict

 3 i=0

 4 while i<3:

 5     peopleInfo['name'] = input('Input Name')

 6     peopleInfo['phone'] = input('Input Phone Number')

 7     phoneBook.append(peopleInfo.copy()) #copy 若不用copy则列表中将全是最后一个人

 8     i+=1

 9 i=0

10 for peopleInfo in phoneBook:

11     print('%d : %s -> phone :%s'%(i, peopleInfo['name'], peopleInfo['phone']))

12     i += 1

来源http://www.cnblogs.com/taowen/articles/11239.aspx

 

 

你可能感兴趣的:(python)