Python 如何将两组列表合并到词典中

Python 如何将两组列表合并到词典中,python,dictionary,Python,Dictionary,我有两组具有相似数据的列表,需要将它们合并到字典中。列表必须包含两组信息,在另一组有“-”的地方填写。到目前为止,这就是我所拥有的 aList = [['Last Name', 'First Name', '2000', '2012'], ['Roberts', 'Gloria', '-', '123000'], ['Arreguin', 'Jeffrey', '81000', '-'], ['Myers', 'George', '-', '860

我有两组具有相似数据的列表,需要将它们合并到字典中。列表必须包含两组信息,在另一组有“-”的地方填写。到目前为止,这就是我所拥有的

aList = [['Last Name', 'First Name', '2000', '2012'], 
       ['Roberts', 'Gloria', '-', '123000'], 
       ['Arreguin', 'Jeffrey', '81000', '-'], 
       ['Myers', 'George', '-', '86000'], 
       ['Willis', 'Mabel', '112000', '-'], 
       ['Oneal', 'Kevin', '96000', '77000'], 
       ['Paz', 'Barbara', '-', '77000'], 
       ['Franklin', 'Claude', '94000', '-'], 
       ['Bradley', 'Shannon', '89000', '-'], 
       ['Fix', 'Anna', '76000', '126000'], 
       ['Meyer', 'Loretta', '-', '116000'], 
       ['Daniels', 'Christina', '85000', '-'], 
       ['Graham', 'Veronica', '-', '136000']]

newList = [['Last Name', 'First Name', '2000', '2012'], 
       ['Meyer', 'Loretta', '123000', '-'], 
       ['Arreguin', 'Jeffrey', '81000', '-'], 
       ['Mielke', 'George', '137000', '-'], 
       ['Thomas', 'Lewis', '132000', '-'], 
       ['Harper', 'Crystal', '80000', '-'], 
       ['Young', 'Gary', '-', '94000'], 
       ['Franklin', 'Claude', '94000', '-'], 
       ['Hedrick', 'James', '-', '105000'], 
       ['Bradley', 'Shannon', '89000', '-'], 
       ['Thigpen', 'Michael', '79000', '-'], 
       ['Willis', 'Mabel', '112000', '-'], 
       ['Hullinger', 'Molly', '70000', '-'], 
       ['Myers', 'George', '-', '86000'], 
       ['Paz', 'Barbara', '-', '77000'], 
       ['Edwards', 'Kathryn', '117000', '97000'], 
       ['Roberts', 'Gloria', '-', '123000'], 
       ['Daniels', 'Christina', '-', '137000'], 
       ['Graham', 'Veronica', '-', '136000']]

def mergeData(newList,aList):
    aDict={}
    for item in range(1,len(newList)):
        key=(newList[item][0],newList[item][1])
        value=(newList[item][2],newList[item][3])
        aDict[key]=value

    for item in range(1,len(aList)):
        #stuck here not sure if im going right way with this 


print(aDict)
这是一个样品

newList= ['Meyer', 'Loretta', '123000', '-']
aList=['Meyer', 'Loretta', '-', '116000']
因此,组合词典应该是

{(‘Meyer’, ‘Loretta’): (‘123000’,’116000’)}

对于此条目。

只需在第一个列表上循环,即可构建具有dict理解能力的词典:

aDict = {(e[0], e[1]): (e[2], e[3]) for e in aList[1:]}
下一个循环覆盖另一个循环,使用辅助函数根据
-
值选择一个或另一个循环:

def pick(vals):
    return vals[1] if vals[0] == '-' else vals[0]

for e in newList:
    key = (e[0], e[1])
    existing = aDict.get(key, ('-', '-'))
    value = (e[2], e[3])
    aDict[key] = tuple(map(pick, zip(existing, value)))
对于您第一次输入的结果:

{('Arreguin', 'Jeffrey'): ('81000', '-'),
 ('Bradley', 'Shannon'): ('89000', '-'),
 ('Daniels', 'Christina'): ('85000', '137000'),
 ('Edwards', 'Kathryn'): ('117000', '97000'),
 ('Fix', 'Anna'): ('76000', '126000'),
 ('Franklin', 'Claude'): ('94000', '-'),
 ('Graham', 'Veronica'): ('-', '136000'),
 ('Harper', 'Crystal'): ('80000', '-'),
 ('Hedrick', 'James'): ('-', '105000'),
 ('Hullinger', 'Molly'): ('70000', '-'),
 ('Last Name', 'First Name'): ('2000', '2012'),
 ('Meyer', 'Loretta'): ('123000', '116000'),
 ('Mielke', 'George'): ('137000', '-'),
 ('Myers', 'George'): ('-', '86000'),
 ('Oneal', 'Kevin'): ('96000', '77000'),
 ('Paz', 'Barbara'): ('-', '77000'),
 ('Roberts', 'Gloria'): ('-', '123000'),
 ('Thigpen', 'Michael'): ('79000', '-'),
 ('Thomas', 'Lewis'): ('132000', '-'),
 ('Willis', 'Mabel'): ('112000', '-'),
 ('Young', 'Gary'): ('-', '94000')}
在您的合并功能中,所有内容再次组合在一起:

def pick(vals):
    return vals[1] if vals[0] == '-' else vals[0]

def mergeData(newList, aList):
    aDict = {(e[0], e[1]): (e[2], e[3]) for e in aList[1:]}

    for e in newList:
        key = (e[0], e[1])
        existing = aDict.get(key, ('-', '-'))
        value = (e[2], e[3])
        aDict[key] = tuple(map(pick, zip(existing, value)))

    return aDict

而且我的字典不包括两个列表中的第一项您的范围从1开始,而不是0;python列表使用基于0的索引。@Martijin Pieters我刚才评论了为什么您能准确地向我们展示您期望的输出?(另外,使用较小的输入数据样本集、减少水平滚动条操作并使其更易于解释可能会有所帮助。)好的,这很有帮助。那么对于像Jeffrey Arreguin这样的人,如果两个列表都有一个左值(而且两个列表都没有右值,但问题似乎不那么严重),会发生什么呢?我们是否可以假设,如果它出现在这两者中,它一定是相同的,或者我们可以任意选择其中一个,或者类似的东西?我不确定这是否是他想要的(我真的不理解这个问题),但如果是,那么肯定应该是
aList[1:][newList[1:][/code>(或者
chain(islice(aList,1,None),islice(newList,1,None)
。无论如何,我认为这不是他想要的原因,因为他有两个列表中出现的键(如
'Arreguin','Jeffrey'
),以及关于“两组信息都填写,而另一组有“-”“,这……一定有什么意义。我得出了同样的结论,所以我现在扩展它。我可以猜它对克里斯蒂娜·丹尼尔斯有什么作用:
('85000','137000')
。但是杰弗里·阿瑞金呢?它只是
('81000','-'))
出现在两个列表中吗?@abarnert yes如果两个列表都包含-对于一个条目,只需保留连字符即可