下面的處理是爲了解析變量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
我不知道這是否是你的問題的原因,但是'S = H = D = C = {}'幾乎肯定不會做你想要的。它將所有這些名稱引用到同一個字典中。我猜你希望他們每個人都是單獨的字典。 – Blckknght
這正是問題所在。 – zerowords