2016-05-16 17 views
0

我正在研究打破一對一映射密碼的程序,其中當前狀態存儲在包含每個字母的可能映射的字典中。每個字母鍵都包含可能映射到的字母列表。最後,每個字母的列表中只應該有一個字母。對於這個問題,該詞典是這樣與相應的(鍵:值)對:PYTHON:如何使用存儲每個可能的字母映射組合的字典創建每個可能字母映射的列表?

'A' : ['A'] 
'B' : ['B', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'V', 'W', 'Z'] 
'C' : ['C'] 
'D' : ['D'] 
'E' : ['E'] 
'F' : ['B', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'V', 'W', 'Z'] 
'G' : ['G', 'W'] 
'H' : ['B', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'V', 'W', 'Z'] 
'I' : ['I'] 
'J' : ['B', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'V', 'W', 'Z'] 
'K' : ['B', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'V', 'W', 'Z'] 
'L' : ['B', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'V', 'W', 'Z'] 
'M' : ['B', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'V', 'W', 'Z'] 
'N' : ['N'] 
'O' : ['O'] 
'P' : ['P'] 
'Q' : ['Q'] 
'R' : ['R'] 
'S' : ['S'] 
'T' : ['T'] 
'U' : ['U'] 
'V' : ['B', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'V', 'W', 'Z'] 
'W' : ['B', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'V', 'W', 'Z'] 
'X' : ['X'] 
'Y' : ['Y'] 
'Z' : ['B', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'V', 'W', 'Z'] 

我將如何創建一個包含所有可能的映射情況作爲元素的列表?這樣的列表將包含每個可能的字典,其中每個字母鍵在其列表中只有一個字母值。這將有助於找到與當前狀態的所有可能的映射。一個例子元素將是詞典:

'A' : ['A'] 
'B' : ['B'] 
'C' : ['C'] 
'D' : ['D'] 
'E' : ['E'] 
'F' : ['F'] 
'G' : ['G'] 
'H' : ['H'] 
'I' : ['I'] 
'J' : ['J'] 
'K' : ['K'] 
'L' : ['L'] 
'M' : ['M'] 
'N' : ['N'] 
'O' : ['O'] 
'P' : ['P'] 
'Q' : ['Q'] 
'R' : ['R'] 
'S' : ['S'] 
'T' : ['T'] 
'U' : ['U'] 
'V' : ['V'] 
'W' : ['W'] 
'X' : ['X'] 
'Y' : ['Y'] 
'Z' : ['Z'] 
+1

請詳細點嗎? – TheLazyScripter

+0

我編輯了第一段。我希望這有助於更詳細地闡述它。 – JoshSchellenberger

+1

有51,874,849,202個單字母映射可以通過從您定義的字典中繪製生成。即使每個映射都以26 * 2個字符存儲,這將需要2.7TB的存儲空間。 – MRule

回答

0

編輯:我MRule同意。共有51,874,849,202個單字母映射。考慮下面的方法(在Python 2.7):

import itertools 
from collections import OrderedDict 
import string 
seed = { 
'A' : ['A'], 
'B' : ['B', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'V', 'W', 'Z'], 
'C' : ['C'], 
'D' : ['D'], 
'E' : ['E'], 
'F' : ['B', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'V', 'W', 'Z'], 
'G' : ['G', 'W'], 
'H' : ['B', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'V', 'W', 'Z'], 
'I' : ['I'], 
'J' : ['B', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'V', 'W', 'Z'], 
'K' : ['B', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'V', 'W', 'Z'], 
'L' : ['B', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'V', 'W', 'Z'], 
'M' : ['B', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'V', 'W', 'Z'], 
'N' : ['N'], 
'O' : ['O'], 
'P' : ['P'], 
'Q' : ['Q'], 
'R' : ['R'], 
'S' : ['S'], 
'T' : ['T'], 
'U' : ['U'], 
'V' : ['B', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'V', 'W', 'Z'], 
'W' : ['B', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'V', 'W', 'Z'], 
'X' : ['X'], 
'Y' : ['Y'], 
'Z' : ['B', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'V', 'W', 'Z'] 
} 
d = OrderedDict(sorted(seed.items(), key=lambda t: t[0])) 
listOfList = d.values() 
for i in itertools.product(* listOfList): 
    # print the possible dict 
    print dict(zip(string.ascii_uppercase, i)) 

UPDATE:To only calculate the possible dictionaries where every mapped letter is unique,你可以這樣做:

import itertools 
import string 
others = ['B', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'V', 'W', 'Z'] 
# this dict is fixed 
dict1 = {k : [k] for k in string.uppercase if k not in others} 
# iterate all possibles in others, then merge two dicts into one 
for i in itertools.permutations(others): 
    dict2 = dict(zip(others, i)) 
    print dict(dict1.items() + dict2.items()) 
+0

這有些幫助。然而,由於沒有一個字母可以被映射到不止一次,是否有一種方法可以計算每個映射字母都是唯一的可能的字典?這將大大減少可能性的數量。 – JoshSchellenberger

+0

增加了示例代碼。那是你要的嗎? – Quinn

+0

是的,太棒了!謝謝! – JoshSchellenberger