您可以使用理解,但仍然有点复杂。
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])