簡體   English   中英

Python3。嘗試使用3個列表創建字典

[英]Python 3. Trying to create a dictionary using 3 lists

我目前正在做一個學校項目,在課程結束時遇到了困難。

def index_district():
  dist = input("Please enter district: ")
  district=[dist]
  candidates = []
  var1 = 1
  while var1:
    var1 += 1
    candidates_names = input('Enter name of candidate for the district.  Hit enter again to end  ')
    if candidates_names == '':
        break
    candidates.append(candidates_names)

  votes = []
  var2 = 1
  while var2:
    var2 += 1
    candidates_votes = input('Enter number of votes for the district.  Hit enter again to end  ')
    if candidates_votes == '':
        break
    votes.append(candidates_votes)

  parties = []
  var3 = 1
  while var3:
    var3 += 1
    candidates_parties = input('Enter parties for the district.  Hit enter again to end  ')
    if candidates_parties == '':
        break
    parties.append(candidates_parties)



  canidate_parties = dict(zip(parties, candidates))
  party_votes = dict(zip(parties, votes))
  dic = dict(zip(district, zip(canidate_parties, party_votes)))
  print(dic)

index_district()

我的輸出是

Please enter district: qwe
Enter name of candidate for the district.  Hit enter again to end  as
Enter name of candidate for the district.  Hit enter again to end  ds
Enter name of candidate for the district.  Hit enter again to end  xc
Enter name of candidate for the district.  Hit enter again to end  
Enter number of votes for the district.  Hit enter again to end  123
Enter number of votes for the district.  Hit enter again to end  234
Enter number of votes for the district.  Hit enter again to end  345
Enter number of votes for the district.  Hit enter again to end  
Enter parties for the district.  Hit enter again to end  rrr
Enter parties for the district.  Hit enter again to end  eee
Enter parties for the district.  Hit enter again to end  www
Enter parties for the district.  Hit enter again to end  
{'qwe': ('rrr', 'rrr')}

Process finished with exit code 0

但是我的最終代碼應該看起來像這樣(我使用教授提供的示例):

{’Name ’: ’Humboldt ’,
’Candidates ’: {’LIB ’: ’Elliott ’, ’NDP ’: ’Angela ’, ’SK ’: ’Mr. Robot ’}, 
’Votes ’: {’LIB ’: 2732 , ’NDP ’: 101 , ’SK ’: 370}
}

我不理解的是如何使列表以我想要的方式工作。 任何幫助,將不勝感激。

該程序的一小部分工作正常:

def create_mapping():

 keys = []
 i = 0
 while 1:
     i += 1
     item = input('Enter party.  Hit enter again to end  ' )
     if item == '':
         break
     keys.append(item)

 votes = []
 x = 0
 while 1:
     x += 1
     partyvotes = input('Enter number of votes.  Hit enter again to end  ')
     if partyvotes == '':
         break
     votes.append(partyvotes)

 dic = dict(zip(keys, votes))
 length = len(keys)

 print (dic)

create_mapping()

這給了我輸出

Enter party.  Hit enter again to end  red
Enter party.  Hit enter again to end  blue
Enter party.  Hit enter again to end  green
Enter party.  Hit enter again to end  
Enter number of votes.  Hit enter again to end  123
Enter number of votes.  Hit enter again to end  234
Enter number of votes.  Hit enter again to end  345
Enter number of votes.  Hit enter again to end  
{'red': '123', 'blue': '234', 'green': '345'}

但是,為什么在更大的程序中不也如此呢?

這不是錯誤。 只是您沒有使用正確的方法來生成輸出。

我建議您逐行遵循代碼:

canidate_parties = dict(zip(parties, candidates))
# => {'rrr': 'as', 'eee': 'ds', 'www': 'xc'}

party_votes = dict(zip(parties, votes))
# => {'rrr': 123, 'eee': 234, 'www': 345}

# This is wrong part
dic = dict(zip(district, zip(canidate_parties, party_votes)))
# zip(canidate_parties, party_votes)
# => [('rrr', 'rrr'), ('eee', 'eee'), ('www', 'www')]
# zip(district, zip(canidate_parties, party_votes)))
# => [('qwe', ('rrr', 'rrr'))]
# dict(zip(district, zip(canidate_parties, party_votes))))
# => {'qwe': ('rrr', 'rrr')}

我想你可以明白一點。 了解有關dict()zip()更多信息將對您有所幫助。 如果要輸出,則需要更改dic = ...行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM