简体   繁体   English

蟒蛇。 尝试将3个列表制作成字典

[英]Python. Trying to make 3 lists into a dictionary

I've asked this question here before, and i've fixed up my code a bit, but im still having problems with the output. 我之前在这里问过这个问题,我已经修正了我的代码,但是即时通讯仍然存在输出问题。 This is for a project for school, so im not after answers, but I do need some help. 这是用于学校的项目,因此即时通讯没有答案,但我确实需要一些帮助。

This is my desiered output 这是我想要的输出

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

My current output is this: 我当前的输出是这样的:

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

My code looks like this: 我的代码如下所示:

def index_district(list1, list2, list3):
 import numpy

 name = ["Name"]

 district = [list2[0]]

 dis = dict(zip(name, district))

 list1.pop(0)

 list2.pop(0)

 canidate_parties = dict(zip(list3 , list2))

 party_votes = dict(zip(list3, list1))

 zipped_dic = dict(zip( canidate_parties,party_votes))

 dic = dict(zip(dis, zipped_dic))

 print (dis)

 print (canidate_parties)

 print (party_votes)


 A = numpy.array((dis, canidate_parties, party_votes))

index_district(['Humboldt',2732,101,370],['Humboldt','Elliott','Angela','Mr. Robot'],['LIB','NDP','SK'])

I've looked online but i cant seem to figure out how to do this. 我已经看过网上了,但似乎无法弄清楚该怎么做。 I'm not looking for answers, but if anyone could offer some help that would be appreciated. 我不是在寻找答案,但是如果有人可以提供一些帮助,我们将不胜感激。 Thanks for your time. 谢谢你的时间。

This worked for me. 这对我有用。

def index_district(list1, list2, list3):
    dictionary = {}
    name = dictionary['Name'] = list1.pop(0)
    list2.pop(0)
    zip1 = dictionary['Candidates'] = dict(zip(list3, list2))
    zip2 = dictionary['Votes'] = dict(zip(list3, list1))
    print(dictionary)

You never assigned the key of 'Candidates' and 'Votes' to their respective values. 您从未将“候选人”和“投票”的键分配给它们各自的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM