2017-03-07 82 views
0

我需要編寫一個程序,將打印到屏幕上的所有可能通過具有前綴555的800電話號碼生成的單詞(以及相關號碼)。請記住,數字1和0在鍵盤上沒有與它們相關的字母。Python電話號碼程序

我試圖到目前爲止如下:

alph_num_dict = {'a': '2', 'b': '2', 'c': '2', 
      'd': '3', 'e': '3', 'f': '3', 
      'g': '4', 'h': '4', 'i': '4', 
      'j': '5', 'k': '5', 'l': '5', 
      'm': '6', 'n': '6', 'o': '6', 
      'p': '7', 'q': '7', 'r': '7', 's': '7', 
      't': '8', 'u': '8', 'v': '8', 
      'w': '9', 'x': '9', 'y': '9', 'z': '9'} 

for letter, digit in alph_num_dict.items(): 
    print ("1.800.555." + str(digit) + str(digit) + str(digit) + str(digit), end=" ") 
    print (" 1.800.555." + str(letter) + str(letter) + str(letter) + str(letter)) 

,我得到的輸出是:

1.800.555.2222  1.800.555.aaaa 
1.800.555.2222  1.800.555.bbbb 
1.800.555.2222  1.800.555.cccc 
1.800.555.3333  1.800.555.dddd 
1.800.555.3333  1.800.555.eeee 
1.800.555.3333  1.800.555.ffff 
1.800.555.4444  1.800.555.gggg 
1.800.555.4444  1.800.555.hhhh 
1.800.555.4444  1.800.555.iiii 
1.800.555.5555  1.800.555.jjjj 
1.800.555.5555  1.800.555.kkkk 
1.800.555.5555  1.800.555.llll 
1.800.555.6666  1.800.555.mmmm 
1.800.555.6666  1.800.555.nnnn 
1.800.555.6666  1.800.555.oooo 
1.800.555.7777  1.800.555.pppp 
1.800.555.7777  1.800.555.qqqq 
1.800.555.7777  1.800.555.rrrr 
1.800.555.7777  1.800.555.ssss 
1.800.555.8888  1.800.555.tttt 
1.800.555.8888  1.800.555.uuuu 
1.800.555.8888  1.800.555.vvvv 
1.800.555.9999  1.800.555.wwww 
1.800.555.9999  1.800.555.xxxx 
1.800.555.9999  1.800.555.yyyy 
1.800.555.9999  1.800.555.zzzz 

這是唯一我需要輸出的一部分。我需要能夠輸出所有其他可能的組合(其中有456,976個)。 我無法弄清楚如何獲得所有可能組合的輸出。

要明確的事情了,我需要的輸出開始尋找類似:

1.800.555.2222  1.800.555.aaaa 
1.800.555.2222  1.800.555.aaab 
1.800.555.2222  1.800.555.aaac 
1.800.555.2223  1.800.555.aaad 
1.800.555.2223  1.800.555.aaae 
1.800.555.2223  1.800.555.aaaf 

任何幫助,這將不勝感激。

+1

http://stackoverflow.com/questions/464864/how-to-get-all-possible-combinations-of-a-list-s-elements –

回答

1

你可能想看看排列函數。

alph_num_dict = {'a': '2', 'b': '2', 'c': '2', 
      'd': '3', 'e': '3', 'f': '3', 
      'g': '4', 'h': '4', 'i': '4', 
      'j': '5', 'k': '5', 'l': '5', 
      'm': '6', 'n': '6', 'o': '6', 
      'p': '7', 'q': '7', 'r': '7', 's': '7', 
      't': '8', 'u': '8', 'v': '8', 
      'w': '9', 'x': '9', 'y': '9', 'z': '9'} 

from itertools import permutations 

all_numbers = permutations(alph_num_dict, 4): 
for number in all_numbers: 
    # TODO: Iterate through all_numbers and do translation