元组中的python组项,没有重复项

时间:2013-08-04 18:06:47

标签: python

我有

(('A', '1', 'UTC\xb100:00'), ('B', '1', 'UTC+01:00'), ('C', '1', 'UTC+02:00'), ('D', '1', 'UTC+01:00'), ('E', '1', 'UTC\xb100:00'), ('F', '1', 'UTC+03:00'))

并希望

  (('A','E, '1', 'UTC\xb100:00'), ('B','D', '1', 'UTC+01:00'), ('C', '1', 'UTC+02:00'), ('F', '1', 'UTC+03:00'))

我见过你可以用清单做到这一点,但是我没有看到这是用turple完成的..这可能吗??

5 个答案:

答案 0 :(得分:1)

您可以使用groupby,但您需要先对输入进行排序,如下所示:

from itertools import groupby
from operator import itemgetter

l = (('A', '1', 'UTC\xb100:00'), ('B', '1', 'UTC+01:00'), ('C', '1', 'UTC+02:00'), ('D', '1', 'UTC+01:00'), ('E', '1', 'UTC\xb100:00'), ('F', '1', 'UTC+03:00'))

result = []
key_items = itemgetter(1, 2)
for key, group in groupby(sorted(l, key=key_items), key=key_items):
    item = []
    item.extend([k[0] for k in group])
    item.extend(key)
    result.append(tuple(item))

print tuple(result)

此代码打印:

(('B', 'D', '1', 'UTC+01:00'), ('C', '1', 'UTC+02:00'), ('F', '1', 'UTC+03:00'), ('A', 'E', '1', 'UTC\xb100:00'))

我理解并不是那么美丽。

答案 1 :(得分:0)

为此使用pandas可能有点过分,但您可以:

import pandas as pd

# somehow, pandas 0.12.0 does prefers 
# a list of tuples rather than a tuple of tuples
t = [('A', '1', 'UTC\xb100:00'), 
     ('B', '1', 'UTC+01:00'), 
     ('C', '1', 'UTC+02:00'), 
     ('D', '1', 'UTC+01:00'), 
     ('E', '1', 'UTC\xb100:00'), 
     ('F', '1', 'UTC+03:00')]

df = pd.DataFrame(t, columns=('letter', 'digit', 'tz'))
grouped = df.groupby('tz')

print(grouped.groups)

# {'UTC+01:00': [1, 3],
#  'UTC+02:00': [2],
#  'UTC+03:00': [5],
#  'UTC\xb100:00': [0, 4]}

merged = []
for key, vals in grouped.groups.iteritems():
    update = [ t[idx][0] for idx in vals ] # add the letters
    update += t[idx][1:] # add the digit and the TZ
    merged.append(update)

print(merged)
# [['F', '1', 'UTC+03:00'], ['C', '1', 'UTC+02:00'], \
#  ['A', 'E', '1', 'UTC\xb100:00'], ['B', 'D', '1', 'UTC+01:00']]

上行是一个相当简洁的df.groupby('tz'),下行是一个相当重的依赖(熊猫加其依赖)。

可以将合并压缩成一条不易理解的行:

merged = [[t[idx][0] for idx in vs] + list(t[idx][1:])
            for vs in grouped.groups.values()]

答案 2 :(得分:0)

你可以使用理解,但仍然有点复杂。

tuples = (('A', '1', 'UTC\xb100:00'), ('B', '1', 'UTC+01:00'), ('C', '1', 'UTC+02:00'), ('D', '1', 'UTC+01:00'), ('E', '1', 'UTC\xb100:00'), ('F', '1', 'UTC+03:00'))

>>values = set(map(lambda x:x[1:3], tuples))
set([('1', 'UTC+03:00'), ('1', 'UTC\xb100:00'), ('1', 'UTC+01:00'), ('1', 'UTC+02:00')])

>>f = [[y[0] for y in tuples if y[1:3]==x] for x in values]
[['F'], ['A', 'E'], ['B', 'D'], ['C']]

>>r = zip((tuple(t) for t in f), values)
[(('F',), ('1', 'UTC+03:00')), (('A', 'E'), ('1', 'UTC\xb100:00')), (('B', 'D'), ('1', 'UTC+01:00')), (('C',), ('1', 'UTC+02:00'))]

>>result = tuple([sum(e, ()) for e in r])
(('F', '1', 'UTC+03:00'), ('A', 'E', '1', 'UTC\xb100:00'), ('B', 'D', '1', 'UTC+01:00'), ('C', '1', 'UTC+02:00'))

把它放在一起:

values = set(map(lambda x:x[1:3], tuples))
f = [[y[0] for y in tuples if y[1:3]==x] for x in values]
r = zip((tuple(t) for t in f), values)
result = tuple([sum(e, ()) for e in r])

答案 3 :(得分:0)

使用元组不允许修改内容,但您可以连接元组以获取其他元组。

def process(data):
    res = []
    for L in sorted(data, key=lambda x:x[2][-5:]):
        if res and res[-1][2][-5:] == L[2][-5:]:
            # Same group... do the merge
            res[-1] = res[-1][:-2] + (L[0],) + res[-1][-2:]
        else:
            # Different group
            res.append(L)
    return res

最终结果在我看来更像是一个列表(逻辑上是同质的内容),而不是一个元组,但如果你真的需要一个元组,你可以只改为return tuple(res)

答案 4 :(得分:0)

如果你只关心具有相同代码的项目在同一个元组中,那么这个答案是有效的:

nodup = {}
my_group_of_items = (('A', '1', 'UTC\xb100:00'), ('B', '1', 'UTC+01:00'), ('C', '1', 'UTC+02:00'), ('D', '1', 'UTC+01:00'), ('E', '1', 'UTC\xb100:00'), ('F', '1', 'UTC+03:00'))
for r in my_group_of_items:
    if r[-1] not in nodup: nodup[r[-1]] = set()
    nodup[r[-1]] |= set(r[:-1])

result = [ tuple(list(nodup[t])+[t]) for t in nodup ]
print result