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_Tuples - Fatal编程技术网

联接列表由在python中具有相同值的多个列表组成

联接列表由在python中具有相同值的多个列表组成,python,list,tuples,Python,List,Tuples,我有一份清单: [['13/03/2012', ['a']], ['13/03/2012', ['b', 'c', 'd']], ['13/03/2012', ['e', 'f']], ['26/03/2012', ['f']], ['02/04/2012', ['a']], ['09/04/2012', ['b']]] 我需要在每个列表中包含相同的日期,第一个值将连接第二个值以这样输出 [['13/03/2012', ['a', 'b', 'c', 'd', 'e', 'f']], ['26

我有一份清单:

[['13/03/2012', ['a']], ['13/03/2012', ['b', 'c', 'd']], ['13/03/2012', ['e', 'f']], ['26/03/2012', ['f']], ['02/04/2012', ['a']], ['09/04/2012', ['b']]]
我需要在每个列表中包含相同的日期,第一个值将连接第二个值以这样输出

[['13/03/2012', ['a', 'b', 'c', 'd', 'e', 'f']], ['26/03/2012', ['f']], ['02/04/2012', ['a']], ['09/04/2012', ['b']]]

有谁能帮我吗?

我建议你先浏览这些链接

这应该能帮你渡过难关

In [7]: [['13/03/2012', ['a']], ['13/03/2012', ['b', 'c', 'd']], ['13/03/2012', ['e', 'f']], ['26/03/2012', ['f']], ['02/04/2012', ['a']], ['09/04/2012', ['b']]]

In [8]: d= {}

In [9]: for item in l:
   ...:     if d.has_key(item[0]):
   ...:         d[item[0]].extend(item[1])
   ...:     else:
   ...:         d[item[0]] = item[1]
   ...:

In [10]: d
Out[10]:
{'02/04/2012': ['a'],
 '09/04/2012': ['b'],
 '13/03/2012': ['a', 'b', 'c', 'd', 'e', 'f'],
 '26/03/2012': ['f']}

In [11]: [[k,v] for k,v in d.items()]
Out[11]:
[['02/04/2012', ['a']],
 ['09/04/2012', ['b']],
 ['26/03/2012', ['f']],
 ['13/03/2012', ['a', 'b', 'c', 'd', 'e', 'f']]]

您可以尝试使用
itertools
。这将按日期对列表进行分组,然后遍历键/组,创建一个以键作为第一个元素和“展平”列表值的列表:

In [51]: from itertools import groupby

In [52]: result = []

In [53]: for key, group in groupby(l, key=lambda x: x[0]):
   ....:     inner = [key, [item for subg in group for item in subg[1]]]
   ....:     result.append(inner)
   ....:
   ....:

In [54]: result
Out[54]:
[['13/03/2012', ['a', 'b', 'c', 'd', 'e', 'f']],
 ['26/03/2012', ['f']],
 ['02/04/2012', ['a']],
 ['09/04/2012', ['b']]]
您可以将其作为一行,但除了超过80个字符外,它的可读性甚至不如第一个版本,因此可能应该避免:)


与avasal类似,但这似乎是使用defaultdict的好地方

from collections import defaultdict
l = [['13/03/2012', ['a', 'b', 'c', 'd', 'e', 'f']], ['26/03/2012', ['f']], ['02/04/2012', ['a']], ['09/04/2012', ['b']]]
d = defaultdict(list)
for item in l:
    d[item[0]].extend(item[1])

print map(list, d.items())

啊!我正在写这个,然后我的电脑决定停止工作。。。回答得很好(+1)@mgilson哈哈,谢谢-我想它已经准备好出现在某个地方了!这就是我在构建列表时要做的事情(如果可能的话)。然后从字典中,如果仍然需要列表,我将从字典转换为列表对象。还将日期格式更改为“%Y/%m/%d”,以便更好地进行排序。
from collections import defaultdict
l = [['13/03/2012', ['a', 'b', 'c', 'd', 'e', 'f']], ['26/03/2012', ['f']], ['02/04/2012', ['a']], ['09/04/2012', ['b']]]
d = defaultdict(list)
for item in l:
    d[item[0]].extend(item[1])

print map(list, d.items())