2016-04-23 63 views
0

我只是从Stackoverflow学习一个新的方法zip(),但它不能正常工作。我无法将2个列表组合成字典使用zip()

def diction(): 
import random 
import string 
import itertools 



dictionary_key={} 
upper_list = [] 
string_dictionary_upper = string.ascii_uppercase 
for n in string_dictionary_upper: 
    upper_list.append(n) 

upper_list_new = list(random.shuffle(upper_list)) 

dictionary_key = dict(zip(upper_list, upper_list_new)) 


diction() 

错误代码是'NoneType'对象不可迭代'。但我找不到原因。

upper_list_new = upper_list[:] #create a copy 
random.shuffle(upper_list_new) #shuffle the copy 

结果就可以:

+0

'random.shuffle(upper_list)'不*返回*列表 - 它将一个列表重新排序并返回'None' –

+0

谢谢,我可以使用random.simple来返回一个新列表吗? –

回答

1

如果你想创建一个列表的改组副本分两步

1)复制列表

2)洗牌复制这样做与其他列表一起压缩。