我是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)中的所有属性以及行作为列表列表(列表名称为行)的所有属性。然后对于每条记录,我与其他记录进行循环,比较每个元素并获取不同元素的索引以获取属性名称。然后最后将它们转换为set。我已经在下面给出了代码,但是问题是,我有5万条记录和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']}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句