按每个子列表

时间:2017-02-18 01:36:26

标签: python python-2.7 list dictionary comparison

我有一个包含列表的字典。例如,

{1: [[sender11, receiver11, text11, address11]], 
 2: [[sender21, receiver21, text21, address21], [sender22, receiver22, text22, address22]], 
 3: [[sender31, receiver31, text31, address31], [sender32, receiver32, text32, address32], [sender33, receiver33, text33, address33]]
 4: [[sender41, receiver41, text41, address41], [sender42, receiver42, text42, address42], [sender43, receiver43, text43, address43], [sender44, receiver44, text44, address44]]}

我想要做的是,对于包含具有2个或更多元素的列表的字典元素(在此示例中为dict[2]dict[3]dict[4]),我做了比较每个列表值的sender, receiver, text。对于具有相同sender, receiver, text的每组列表值,我会做一些事情。

例如,在dict[3]中,如果sender31, receiver31, text31sender32, receiver32, text32sender33, receiver33, text33的值相同,那么我将对所有3个列表值执行某些操作

dict[4]中说,如果sender41, receiver41, text41sender42, receiver42, text42的值相同,而sender43, receiver43, text43sender44, receiver44, text44的值相同,但与{sender41, receiver41, text41不同1}},然后我将分别处理这两个组。

我写了一个Python脚本,几乎蛮力比较sender21, receiver21, text21sender22, receiver22, text22的值,即

if sender21 == sender22 and receiver21 == receiver22 and text21 == text22:
   # Do something

这不是有效的,因为它只适用于2个列表值,但我不知道如何实现它,以便它适用于大于1的任意数量的列表值。

1 个答案:

答案 0 :(得分:1)

我认为defaultdict是显而易见的方式:

from collections import defaultdict

def collate(seq):
    groups = defaultdict(list)
    for subseq in seq:
        groups[tuple(subseq[:3])].append(subseq[3])
    return groups

根据您的实际数据,您可以将以上函数中的tuple(subseq[:3])替换为例如(subseq[1], subseq[4], subseq[5])subseq[3],或附加subseq >>> data = [ ... ['S1', 'R1', 'T1', 'A3'], ... ['S2', 'R2', 'T2', 'A4'], ... ['S1', 'R1', 'T1', 'A5'], ... ['S2', 'R2', 'T2', 'A6'] ... ] 本身......这取决于您对数据的处理。

密钥必须是元组而不是列表,因为密钥必须是不可变的。

示例:

>>> collate(data)
defaultdict(<type 'list'>, {
    ('S2', 'R2', 'T2'): ['A4', 'A6'],
    ('S1', 'R1', 'T1'): ['A3', 'A5']
})

>>> for (sender, receiver, text), addresses in collate(data).items():
...     print sender, receiver, text
...     print '|'.join(addresses)
...     print
... 
S2 R2 T2
A4|A6

S1 R1 T1
A3|A5
 

您可以像处理任何其他词典一样使用此功能,例如

b1