Skip to main content
 首页 » 编程设计

python之元组中的逐个元素比较以返回一组集合之python

2024年08月27日34zlslch

我是 python 新手。有人帮我解决这个要求。我有一个数据集,第一行是属性,其余行是记录。

我的要求是将每条记录与其他记录进行比较,并给出不同元素的属性名称。所以最后,我应该有一组集合作为输出。

例如,如果我有 3 条这样的 3 列记录。

         Col1 Col2 Col3 
tuple1    H   C    G 
tuple2    H   M    G 
tuple3    L   M    S 

它应该像这样给我 tuple1,tuple2 = {Col2} tuple1,tuple3 = {Col1,Col2,Col3} tuple2,tuple3 = {Col1,Col3}

最终输出应该是{{Col2},{Col1,Col2,Col3},{Col1,Col3}}

这是我试过的代码,

我现在所做的是,将每一行读入列表。因此,一个列表中的所有属性(列表名称为 list_attr)和行作为列表列表(列表名称为行)。然后对于每条记录,我与其他记录循环,比较每个元素并获取不同元素的索引以获取属性名称。然后最后将它们转换为集合。我已经给出了下面的代码,但问题是,我有 50k 条记录和 15 个属性要处理,所以这个循环需要很长时间才能执行,有没有其他方法可以尽快完成或提高性能。

dis_sets = []   
for l in rows: 
    for l1 in rows: 
        if l != l1: 
            i = 0 
            in_sets = [] 
            while(i < length): 
                if l[i] != l1[i]: 
                    in_sets.append(list_attr[i]) 
                i = i+1 
            if in_sets != []: 
                dis_sets.append(in_sets) 
skt = set(frozenset(temp) for temp in dis_sets) 

请您参考如下方法:

考虑:

>>> tuple1=('H', 'C', 'G') 
>>> tuple2=('H', 'M', 'G') 
>>> tuple3=('L', 'M', 'S') 

好的,你说'我的要求是将每条记录与其他记录进行比较,并给出不同元素的属性名称。'

将其写入代码:

>>> [i for i, t in enumerate(zip(tuple1, tuple2), 1) if t[0]!=t[1]] 
[2] 
>>> [i for i, t in enumerate(zip(tuple1, tuple3), 1) if t[0]!=t[1]] 
[1, 2, 3] 
>>> [i for i, t in enumerate(zip(tuple2, tuple3), 1) if t[0]!=t[1]] 
[1, 3] 

然后你说'最终输出应该是{{Col2},{Col1,Col2,Col3},{Col1,Col3}}

因为一组集合会松散顺序,这没有意义。应该是:

>>> [[i for i, t in enumerate(zip(*pair), 1) if t[0]!=t[1]] for pair in  
...     [(tuple1, tuple2), (tuple1, tuple3), (tuple2, tuple3)]] 
[[2], [1, 2, 3], [1, 3]] 

如果您真的想要集合,您可以将它们作为子元素;如果你有一组真正的集合,你就失去了哪对是哪对的信息。

集合列表:

>>> [{i for i, t in enumerate(zip(*pair), 1) if t[0]!=t[1]} for pair in  
...     [(tuple1, tuple2), (tuple1, tuple3), (tuple2, tuple3)]] 
[set([2]), set([1, 2, 3]), set([1, 3])] 

和您的几乎相同的期望输出:

>>> [{'Col{}'.format(i) for i, t in enumerate(zip(*pair), 1) if t[0]!=t[1]} for pair in  
...     [(tuple1, tuple2), (tuple1, tuple3), (tuple2, tuple3)]] 
[set(['Col2']), set(['Col2', 'Col3', 'Col1']), set(['Col3', 'Col1'])] 

(请注意,由于集合是无序的,因此字符串的顺序会发生变化。如果顶级顺序发生变化,您有什么?)

注意如果你有一个列表列表,你更接近你想要的输出:

>>> [['Col{}'.format(i) for i, t in enumerate(zip(*pair), 1) if t[0]!=t[1]] for pair  
...     in [(tuple1, tuple2), (tuple1, tuple3), (tuple2, tuple3)]] 
[['Col2'], ['Col1', 'Col2', 'Col3'], ['Col1', 'Col3']] 

根据评论编辑

你可以做类似的事情:

def pairs(LoT): 
                   # for production code, consider using a deque of tuples... 
    seen=set()     # hold the pair combinations seen 
    while LoT: 
        f=LoT.pop(0) 
        for e in LoT: 
            se=frozenset([f, e]) 
            if se not in seen: 
                seen.add(se) 
                yield se 
 
 >>> list(pairs([('H', 'C', 'G'), ('H', 'M', 'G'), ('L', 'M', 'S')])) 
 [frozenset([('H', 'M', 'G'), ('H', 'C', 'G')]), frozenset([('L', 'M', 'S'), ('H', 'C', 'G')]), frozenset([('H', 'M', 'G'), ('L', 'M', 'S')])] 

然后可以这样使用:

>>> LoT=[('H', 'C', 'G'), ('H', 'M', 'G'), ('L', 'M', 'S')] 
>>> [['Col{}'.format(i) for i, t in enumerate(zip(*pair), 1) if t[0]!=t[1]] for pair  
...        in pairs(LoT)] 
[['Col2'], ['Col1', 'Col2', 'Col3'], ['Col1', 'Col3']] 

编辑 #2

如果您想要标题与计算值:

>>> theader=['tuple col 1', 'col 2', 'the third' ] 
>>> [[theader[i] for i, t in enumerate(zip(*pair)) if t[0]!=t[1]] for pair 
...       in pairs(LoT)] 
[['col 2'], ['tuple col 1', 'col 2', 'the third'], ['tuple col 1', 'the third']] 

如果您想要(我怀疑是正确的答案)列表的字典列表:

>>> di=[] 
>>> for pair in pairs(LoT):     
...    di.append({repr(list(pair)): [theader[i] for i, t in enumerate(zip(*pair)) if t[0]!=t[1]]}) 
>>> di 
[{"[('H', 'M', 'G'), ('H', 'C', 'G')]": ['col 2']}, {"[('L', 'M', 'S'), ('H', 'C', 'G')]": ['tuple col 1', 'col 2', 'the third']}, {"[('H', 'M', 'G'), ('L', 'M', 'S')]": ['tuple col 1', 'the third']}]    

或者,只是一个直接的列表词典:

>>> di={} 
>>> for pair in pairs(LoT):     
...    di[repr(list(pair))]=[theader[i] for i, t in enumerate(zip(*pair)) if t[0]!=t[1]]   
>>> di 
{"[('H', 'M', 'G'), ('L', 'M', 'S')]": ['tuple col 1', 'the third'], "[('L', 'M', 'S'), ('H', 'C', 'G')]": ['tuple col 1', 'col 2', 'the third'], "[('H', 'M', 'G'), ('H', 'C', 'G')]": ['col 2']}