Python 两个列表合并

有两个列表,分别为:

1
2
names = ['Alice', 'Beth', 'Ceil']
numbers = ['2341', '9102', '3158']

生成一个字典:

book = {'Alice': '2341', 'Beth': '9102', 'Ceil': '3158'}

有两种方法,一个是不断迭代,另外一种就是用zip函数

方法一:

1
2
3
4
5
6
book = {}
i=0
while i<len(names):
phonebook[names[i]]=numbers[i]
i+=1
print book

方法二:

1
2
book = dict(zip(names, numbers))
print phone

Comments

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×