2017-02-15 195 views
0

下面的處理是爲了解析變量dealList填充4字典SHD和C.我認爲問題是在我使用eval(suit)代碼的倒數第二行,西裝是循環通過。是否有替代此代碼評估

所有的西裝SHD,和C初始化爲空的字典,但是是爲了最終包含13項字典,一個在其訴訟每張卡。

我懷疑所發生的事情是,每次的eval(套裝)執行它找到一本字典來處理,但不會保留字典的名稱,因此所需的命名字典不會被更新。換句話說,它看起來像通過使用從列表中取出的字母S循環遍歷,但該字母也命名字典。但我不知道如何告訴蟒蛇這兩者是相關的。

我需要使用什麼來代替eval(),或者除了eval()來完成我的目標?

keys = list('23456789TJQKA') 
values = range (13) 
suitDict = {} 
for key,value in zip(keys, values): 
    suitDict[key] = value 
dealList = 'AQJT5.KQ.8.KQT95 3.A765.QT743.843 974.T93.J92.AJ62 K862.J842.AK65.7'.split() 
players = list('NESW') 
suits = list('SHDC') 
S = H = D = C = {} 
playerHand ={} 
for player,hand in zip(players,dealList): 
    playerHand[player]=hand 
    print player,hand 
    for suit,cards in zip(suits,playerHand[player].split('.')): 
     print "SC:",suit,cards 
     for card in cards: 
      eval(suit)[suitDict[card]]= player 
      print "card",card,"Suit",suit,"X",eval(suit),"card",suitDict[card],"player",player  

也許輸出的列表將顯示問題。請注意,在下面的示例輸出列表中,前5張是黑桃,接下來的2張是心,K和Q都是特定的。但黑桃詞典正在改變,而不是心靈詞典。正在更新的陣列的這種缺乏改變會一直持續下去。

N AQJT5.KQ.8.KQT95 
SC: S AQJT5 
card A Suit S X [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 'N'] card 12 player N 
card Q Suit S X [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'N', 11, 'N'] card 10 player N 
card J Suit S X [0, 1, 2, 3, 4, 5, 6, 7, 8, 'N', 'N', 11, 'N'] card 9 player N 
card T Suit S X [0, 1, 2, 3, 4, 5, 6, 7, 'N', 'N', 'N', 11, 'N'] card 8 player N 
card 5 Suit S X [0, 1, 2, 'N', 4, 5, 6, 7, 'N', 'N', 'N', 11, 'N'] card 3 player N 
SC: H KQ 
card K Suit H X [0, 1, 2, 'N', 4, 5, 6, 7, 'N', 'N', 'N', 'N', 'N'] card 11 player N 
card Q Suit H X [0, 1, 2, 'N', 4, 5, 6, 7, 'N', 'N', 'N', 'N', 'N'] card 10 player N 
SC: D 8 
card 8 Suit D X [0, 1, 2, 'N', 4, 5, 'N', 7, 'N', 'N', 'N', 'N', 'N'] card 6 player N 
SC: C KQT95 
card K Suit C X [0, 1, 2, 'N', 4, 5, 'N', 7, 'N', 'N', 'N', 'N', 'N'] card 11 player N 
card Q Suit C X [0, 1, 2, 'N', 4, 5, 'N', 7, 'N', 'N', 'N', 'N', 'N'] card 10 player N 
card T Suit C X [0, 1, 2, 'N', 4, 5, 'N', 7, 'N', 'N', 'N', 'N', 'N'] card 8 player N 
card 9 Suit C X [0, 1, 2, 'N', 4, 5, 'N', 'N', 'N', 'N', 'N', 'N', 'N'] card 7 player N 
card 5 Suit C X [0, 1, 2, 'N', 4, 5, 'N', 'N', 'N', 'N', 'N', 'N', 'N'] card 3 player N 
E 3.A765.QT743.843 
SC: S 3 
card 3 Suit S X [0, 'E', 2, 'N', 4, 5, 'N', 'N', 'N', 'N', 'N', 'N', 'N'] card 1 player E 
SC: H A765 
card A Suit H X [0, 'E', 2, 'N', 4, 5, 'N', 'N', 'N', 'N', 'N', 'N', 'E'] card 12 player E 
card 7 Suit H X [0, 'E', 2, 'N', 4, 'E', 'N', 'N', 'N', 'N', 'N', 'N', 'E'] card 5 player E 
card 6 Suit H X [0, 'E', 2, 'N', 'E', 'E', 'N', 'N', 'N', 'N', 'N', 'N', 'E'] card 4 player E 
card 5 Suit H X [0, 'E', 2, 'E', 'E', 'E', 'N', 'N', 'N', 'N', 'N', 'N', 'E'] card 3 player E 
SC: D QT743 
card Q Suit D X [0, 'E', 2, 'E', 'E', 'E', 'N', 'N', 'N', 'N', 'E', 'N', 'E'] card 10 player E 
card T Suit D X [0, 'E', 2, 'E', 'E', 'E', 'N', 'N', 'E', 'N', 'E', 'N', 'E'] card 8 player E 
card 7 Suit D X [0, 'E', 2, 'E', 'E', 'E', 'N', 'N', 'E', 'N', 'E', 'N', 'E'] card 5 player E 
card 4 Suit D X [0, 'E', 'E', 'E', 'E', 'E', 'N', 'N', 'E', 'N', 'E', 'N', 'E'] card 2 player E 
card 3 Suit D X [0, 'E', 'E', 'E', 'E', 'E', 'N', 'N', 'E', 'N', 'E', 'N', 'E'] card 1 player E 
SC: C 843 
card 8 Suit C X [0, 'E', 'E', 'E', 'E', 'E', 'E', 'N', 'E', 'N', 'E', 'N', 'E'] card 6 player E 
card 4 Suit C X [0, 'E', 'E', 'E', 'E', 'E', 'E', 'N', 'E', 'N', 'E', 'N', 'E'] card 2 player E 
card 3 Suit C X [0, 'E', 'E', 'E', 'E', 'E', 'E', 'N', 'E', 'N', 'E', 'N', 'E'] card 1 player E 
+1

我不知道這是否是你的問題的原因,但是'S = H = D = C = {}'幾乎肯定不會做你想要的。它將所有這些名稱引用到同一個字典中。我猜你希望他們每個人都是單獨的字典。 – Blckknght

+0

這正是問題所在。 – zerowords

回答

1

您的代碼古怪的主要原因是事實,你的所有衣服詞典(SHDC)引用的是相同的字典。如果您希望它們指向不同的參考(如S, H, D, C = {}, {}, {}, {}),則需要分別初始化它們。

這就是說,eval是(幾乎)從來沒有解決方案。在你的情況,我會做一些事情,如:

keys = list('23456789TJQKA') 
values = range (13) 
suitDict = {} 
for key,value in zip(keys, values): 
    suitDict[key] = value 
dealList = 'AQJT5.KQ.8.KQT95 3.A765.QT743.843 974.T93.J92.AJ62 K862.J842.AK65.7'.split() 
players = list('NESW') 
suits = {"S": {}, "H": {}, "D": {}, "C": {}} 
playerHand = {} 
for player,hand in zip(players,dealList): 
    playerHand[player] = hand 
    print player,hand 
     for suit,cards in zip(suits,playerHand[player].split('.')): 
      print "SC:",suit,cards 
      for card in cards: 
       suits[suit][suitDict[card]] = player 
       print "card",card,"Suit",suit,"X",suits[suit],"card",suitDict[card],"player",player 

話雖這麼說,我覺得你過於複雜你的情況......

+0

你不需要_suits.keys()_在_zip_下 - 只需_suite_就可以。大多數迭代修飾符 - 例如_sorted_,_list_,_set_等 - 處理引擎蓋下的字典鍵。在Python 2. * _keys()_將產生中間列表;在Python 3中。*它只是冗餘代碼。 – volcano

+0

沒錯。我刪除了冗餘。 – zwer

+0

是的。我花了一段時間纔看到你取代了'eval()',這是可取的。你的意思是,即使是另一種選擇會更簡化這個嗎?例如,我正在考慮將套裝SHD&C定義爲範圍(13)(列表,不是字典),並在我的'eval()'過程中將suitDict [card]定義爲一個整數索引。如果我這樣做,我*想*我需要恢復到我原來的'eval()'過程,但它的作用是使SHD&C成爲列表,這對我來說很有好處。註釋? – zerowords