我有元组的列表:名单从列表共享一个值元的所有项目
l = [(x,y,2),(x,y,3),(x,y,4),(x,y,2),(x,y,2),(x,y,3)]
我需要提取共享的最后一个值到元组的列表的列表元组:
nl = [[(x,y,2),(x,y,2),(x,y,2)],[(x,y,3),(x,y,3)]]
我不知道最后的价值当然。
我有元组的列表:名单从列表共享一个值元的所有项目
l = [(x,y,2),(x,y,3),(x,y,4),(x,y,2),(x,y,2),(x,y,3)]
我需要提取共享的最后一个值到元组的列表的列表元组:
nl = [[(x,y,2),(x,y,2),(x,y,2)],[(x,y,3),(x,y,3)]]
我不知道最后的价值当然。
使用GROUPBY首先用相同的lambda排序然后分组。通过列表理解,您可以将所有分组组合在一起,并过滤掉所有长度为1的元素,以摆脱不共享值的元组。
from itertools import groupby
tuples = [(1, 2, 2), (3, 1, 3), (1, 2, 4), (8, 9, 2), (12, 1, 2), (0, 1, 3)]
tuple_tail = lambda (first, mid, last): last
tuples.sort(key=tuple_tail)
print filter(lambda item: len(item) > 1, [list(group) for key, group in groupby(tuples, tuple_tail)])
那么这个人是不是最好的解决方案,但它是一个解决方案。我定义了一些辅助函数
retrieves last of tuple
compares equality of two tuples
。然后写自定义组功能,搜索是通过使用filter
等于所有元素,然后map
跨所有元素以获得与所有可能的分组(所有组)列表。我无法想象如何使用列表理解而不会造成混乱,所以我去了reduce
,并写了一个函数来删除重复和/或长度为1 (fn)的元素。如果您使用set
或者一般来说可能只是一种不同的方法,这肯定会得到优化。希望这可以帮助你找到任何方法将。
tuples = [(1, 2, 2), (3, 1, 3), (1, 2, 4), (8, 9, 2), (12, 1, 2), (0, 1, 3)]
# helper functions
tuple_tail = lambda (first, mid, last): last
is_tuples_equal = lambda tuple1, tuple2: tuple_tail(
tuple1) == tuple_tail(tuple2)
# groups by last (_,_,last)
group_by_last = lambda tuple: filter(
lambda item: is_tuples_equal(item, tuple), tuples)
# get all groupings
group_all = map(group_by_last, tuples)
# if group is not in list and not length of 1 insert into list
fn = lambda acc, val: acc if val in acc or len(val) == 1 else acc + [val]
print reduce(fn, group_all, [])
如果创建一个字典,并使用每个元组为key
的tuple_tail
值,使value
为所有包含该key
作为自己尾巴的元组。然后,您可以使用列表理解积累了字典的值和DIS包括长度的元素小于1
tuples = [(1, 2, 2), (3, 1, 3), (1, 2, 4), (8, 9, 2), (12, 1, 2), (0, 1, 3)]
mydict = dict()
create = lambda tupl: mydict.update({tuple_tail(tupl): [tupl]})
update = lambda tupl: mydict[tuple_tail(tupl)].append(tupl)
tuple_tail = lambda (first, mid, last): last
populate = lambda tupl: update(tupl) if tuple_tail(tupl) in mydict else create(tupl)
map(populate, tuples)
print [tuple for tuple in mydict.values() if len(tuple) > 1]
[[(1, 2, 2), (8, 9, 2), (12, 1, 2)], [(3, 1, 3), (0, 1, 3)]]
首先,根据排序去年元素(可以使用this one)
然后,他们组。您可以简单地使用for loop
。一个伪代码是:
cur_value = list[0][2] #second element of first tuple
llt = []; #list_of_list_of_tuples
for tuple in list:
l_tpl = []
if cur_value == tuple[2]:
l_tpl.append(tuple)
else:
if len(l_tpl) > 1:
llt.append(l_tpl)
if len(l_tpl) > 1:
llt.append(l_tpl) # we need to add the last list of tuples.
我还没有测试,我不确定的语法。
你可以用字典来组项目与同最后一个元素
x,y= 'x','y'
l = [(x,y,2),(x,y,3),(x,y,4),(x,y,2),(x,y,2),(x,y,3)]
res = {}
for item in l:
if item[2] not in res:
res[item[2]] = []
res[item[2]].append(list(item))
print filter(lambda x: len(x) > 1 , res.values())
[['x', 'y', 2], ['x', 'y', 2], ['x', 'y', 2]], [['x', 'y', 3], ['x', 'y', 3]]
或使用熊猫
l = pd.Series([(x,y,2),(x,y,3),(x,y,4),(x,y,2),(x,y,2),(x,y,3) ])
print [ line[1].tolist() for line in l.groupby(lambda x: l[x][2]) if len(line[1]) > 1]
[[('x', 'y', 2), ('x', 'y', 2), ('x', 'y', 2)], [('x', 'y', 3), ('x', 'y', 3)]]
是否可以在不导入任何额外模块的情况下执行此操作?最好作为列表理解? – dyb
@dyb有些想法后,我知道了!一探究竟。 –