简体   繁体   English

使元组列表中的元素在 python 中唯一的有效方法是什么?

[英]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.

相关问题 Python-元组-检索元组列表中的唯一元素 - Python - Tuples - Retrieve unique elements in list of tuples python比较元组列表中项目的有效方法 - python efficient way to compare item in list of tuples Python元组列表:按字典的唯一元素进行组织 - Python list of tuples: organize by unique elements to a dictionary 在python列表中查找元组中的唯一元素 - Find unique elements in tuples in a python list Python将这个列表元组变成字典的最快方法是什么? - Python what is the fastest way to make this list tuples into a dict? Python:由两个元素组成的内存高效的元组列表 - Python: Memory efficient sort of a list of tuples by two elements 计算列表中元组之间距离的最省时的方法是什么? - What is the most time efficient way to calculate the distance between tuples in a list? 计算元组列表中唯一元素的数量,而不管Python中的顺序如何 - Count the number of unique elements of a list of tuples regardless of order in Python 从python中的元组列表中删除唯一的元组 - Remove unique tuples from a list of tuples in python 什么是计算Python中整数列表中唯一乘法和加法对的数量的有效方法? - What is an efficient way of counting the number of unique multiplicative and additive pairs in a list of integers in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM