[英]What is an efficient way to make elements in a list of tuples unique in python?
Let's say I have a list of tuples假设我有一个元组列表
l = [('A', 12345), ('A', 2435), ('A', 2342), ('B', 2968), ('B', 9483), ('C', 563)]
What is the most efficient way to make the items in my list unique, like this:使我的列表中的项目独一无二的最有效方法是什么,如下所示:
l = [('A.1', 12345), ('A.2', 2435), ('A.3', 2342), ('B.1', 2968), ('B.2', 9483), ('C.1', 563)]
One approach could be to group with itertools.groupby()
and then "expand" the groups:一种方法可能是使用itertools.groupby()
分组,然后“扩展”这些组:
from itertools import groupby
from operator import itemgetter
l = [('A', 12345), ('A', 2435), ('A', 2342), ('B', 2968), ('B', 9483), ('C', 563)]
print([
(f'{k}.{index}', v)
for k, g in groupby(l, itemgetter(0))
for index, (_, v) in enumerate(g, start=1)
])
Prints:印刷:
[('A.1', 12345), ('A.2', 2435), ('A.3', 2342), ('B.1', 2968), ('B.2', 9483), ('C.1', 563)]
Note that for the grouping to work, the input l
needed to be ordered by the grouping key which it seems is the case for this example input.请注意,要使分组起作用,输入l
需要按分组键进行排序,本示例输入似乎就是这种情况。
By request, i've posted a pandas approach to this question:根据要求,我发布了一个熊猫方法来解决这个问题:
import pandas as pd
df = pd.DataFrame(l)
# Create a count per group and add them to the string:
df[0] = df[0] + "." + list(map(str,list(df.groupby(0).cumcount()+1)))
# Transpose the columns to rows so we can aggregate by 2 and create a tuple:
df.T.groupby(np.arange(len(df.T))//2).agg(tuple).to_numpy().tolist()[0]
Output输出
[('A.1', 12345),
('A.2', 2435),
('A.3', 2342),
('B.1', 2968),
('B.2', 9483),
('C.1', 563)]
You could also group with a collections.defaultdict()
, then flatten the result with itertools.chain.from_iterable()
.您还可以使用collections.defaultdict()
分组,然后使用itertools.chain.from_iterable()
将结果展平。 This works whether the result is sorted or not.无论结果是否排序,这都有效。
from collections import defaultdict
from itertools import chain
l = [("A", 12345), ("A", 2435), ("A", 2342), ("B", 2968), ("B", 9483), ("C", 563)]
# First group by first item in tuple
groups = defaultdict(list)
for k, v in l:
groups[k].append(v)
# defaultdict(<class 'list'>, {'A': [12345, 2435, 2342], 'B': [2968, 9483], 'C': [563]})
# Now flatten grouped items into a flat list
result = list(
chain.from_iterable(
(("%s.%d" % (k, i), e) for i, e in enumerate(v, start=1))
for k, v in groups.items()
)
)
print(result)
Output:输出:
[('A.1', 12345), ('A.2', 2435), ('A.3', 2342), ('B.1', 2968), ('B.2', 9483), ('C.1', 563)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.