繁体   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