Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将具有重复键值的两个列表转换为字典_Python_List_Dictionary - Fatal编程技术网

Python 将具有重复键值的两个列表转换为字典

Python 将具有重复键值的两个列表转换为字典,python,list,dictionary,Python,List,Dictionary,我希望我的输出如下所示: list1 = ['10/20/2008', '3/25/2009', '3/26/2009', '3/28/2009', '3/28/2009', '8/23/2009', '8/23/2009'] list2 = [0,0,0,0,1,0,0] 您可以使用: 输出 list1 = ['10/20/2008', '3/25/2009', '3/26/2009', '3/28/2009', '3/28/2009', '8/23/2009', '8/23/2009']

我希望我的输出如下所示:

list1 = ['10/20/2008', '3/25/2009', '3/26/2009', '3/28/2009', '3/28/2009', '8/23/2009', '8/23/2009']
list2 = [0,0,0,0,1,0,0]
您可以使用:

输出

list1 = ['10/20/2008', '3/25/2009', '3/26/2009', '3/28/2009', '3/28/2009', '8/23/2009', '8/23/2009']
list2 = [0, 0, 0, 0, 1, 0, 0]

result = {}
for k, v in zip(list1, list2):
    result.setdefault(k, []).append(v)

print(result)
result = {k: v[0] if len(v) == 1 else v for k, v in result.items()}
如果要将单个项目的列表作为单个项目,请执行以下操作:

{'3/28/2009': [0, 1], '3/26/2009': [0], '10/20/2008': [0], '3/25/2009': [0], '8/23/2009': [0, 0]}
输出

list1 = ['10/20/2008', '3/25/2009', '3/26/2009', '3/28/2009', '3/28/2009', '8/23/2009', '8/23/2009']
list2 = [0, 0, 0, 0, 1, 0, 0]

result = {}
for k, v in zip(list1, list2):
    result.setdefault(k, []).append(v)

print(result)
result = {k: v[0] if len(v) == 1 else v for k, v in result.items()}

一种方法是使用
defaultdict
,如下所示:

{'3/25/2009': 0, '3/26/2009': 0, '8/23/2009': [0, 0], '10/20/2008': 0, '3/28/2009': [0, 1]}

这并不是一个只列出清单的实现,而是应该完成的工作

from collections import defaultdict
res_dict = defaultdict(list)

for k, v in zip(list1, list2):
    res_dict[k].append(v)

您可以使用python内置的
groupby

import numpy as np
listToDict=dict()
npList1=np.array(list1)
npList2=np.array(list2)
for date in set(list1):
    listToDict[date]=list(npList2[[np.where(npList1 == date)[0]]])
结果:

from itertools import groupby

list1 = ['10/20/2008', '3/25/2009', '3/26/2009', '3/28/2009', '3/28/2009', '8/23/2009', '8/23/2009']
list2 = [0, 0, 0, 0, 1, 0, 0]

# Zip and sort the list in order to use them with groupby
list3 = [(x, y) for x, y in zip(list1, list2)]
list3.sort()

result = {}

# Group by date.
for value, grouper in groupby(list3, lambda x: x[0]):
    # key_value has the form (date, zero_or_one)
    key_value = grouper.next()
    # Populate result according requirements. 
    result[value] = key_value[1]
    # If more than one value for date, use a list.
    for index, kv in enumerate(grouper):
        if index == 0:
            result[value] = [result[value], kv[1]]
        else:
            result[value].append(value)

print result

对于所有唯一值都应为整数,而重复值应为列表的要求,您有多坚决?如果您愿意列出所有值,则可以使用一些相当优雅的解决方案。唯一的值的长度仅为1。与OP的期望输出不太匹配,因为
res_dict['10/20/2008']
给出的是
[0]
,而不是
0
。另外,我想你的意思是
来自collections import defaultdict
@Kevin这两个观察结果都是正确的,关于第一个,我认为它在这方面是灵活的,正如你向OP所要求的。关于第二个观察结果,你是正确的,谢谢!