python中元组逐元素比较返回集合的集合

2024-04-25 08:36:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我是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 name是list_attr)和行作为列表的list(list name是rows)。然后,对于每个记录,我将与其他记录循环,比较每个元素并获取不同元素的索引以获得属性名。最后把它们转换成set。我已经给出了下面的代码,但问题是,我有5万条记录和15个属性要处理,所以这个循环需要很长时间才能执行,有没有其他方法可以尽快完成或提高性能。在

^{pr2}$

Tags: 数据name元素列表属性记录listcol2
1条回答
网友
1楼 · 发布于 2024-04-25 08:36:31

考虑:

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

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

把它写进代码里:

^{pr2}$

然后您声明',最后的输出应该是{{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')])]

然后可以这样使用:

^{8}$

编辑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']]

如果你想要(我怀疑答案是正确的)一个列表的Dicts列表:

>>> 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']}

相关问题 更多 >