2016-04-23 76 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)洗牌複製這樣做與其他列表一起壓縮。