简体   繁体   English

如何对列表中的元素进行分组?

[英]How to group elements in a list?

I have a python list: 我有一个python列表:

['AM43',
 'AM22',
 'AM51',
 'AM43',
 'AM22',
 'AM51',
 'AM43',
 'AM22',
 'AM51']

I want the output to be a list: 我希望输出为列表:

['AM43',
 'AM43',
 'AM43',
 'AM22',
 'AM22',
 'AM22',
 'AM51',
 'AM51',
 'AM51']

I tried sort() but that also rearranges the order. 我尝试了sort()但这也重新排列了顺序。 I don't want that. 我不要 I want the output to be in the same order as the input list. 我希望输出与输入列表的顺序相同。

You can create a dict that stores the index of the first occurrence of each value, and use the dict to perform the sort: 您可以创建一个存储每个值首次出现的索引的字典,并使用该字典执行排序:

lst = ["AM43", "AM22", "AM51", "AM43", "AM22", "AM51", "AM43", "AM22", "AM51"]

ix = {k: i for i, k in reversed(list(enumerate(lst)))}
res = sorted(lst, key=ix.get)
# ['AM51', 'AM51', 'AM51', 'AM22', 'AM22', 'AM22', 'AM43', 'AM43', 'AM43']

Edit: @emsimposon92 provides a 2-pass linear-time solution implementable as follows: 编辑:@ emsimposon92提供了一个可通过以下方式实现的两遍线性时间解决方案:

from collections import Counter

ctr = Counter(lst)
visited = set()
res2 = list()
for x in lst:
    if x in visited:
        continue
    res2.extend([x] * ctr[x])
    visited.add(x)

您可以为每个唯一编号创建一个列表,然后根据原始输入顺序将它们串联起来。

Following Yakym Pirozhenko's answer, to get the appropriate order you need to reverse-enumerate lst when building the index dict: 按照Yakym Pirozhenko的回答,要获得适当的顺序,您需要在构建索引dict时反向枚举lst

lst = ["AM43", "AM22", "AM51", "AM43", "AM22", "AM51", "AM43", "AM22", "AM43"]

ix = {k: i for i, k in zip(range(len(lst), -1, -1), reversed(lst))}

res = sorted(lst, key=ix.get)

Here's a solution using collections.Counter , itertools and toolz.unique . 这是使用collections.Counteritertoolstoolz.unique的解决方案。 Note the last uses a 3rd party library, but the source code is just an itertools unique_everseen recipe . 请注意,最后一个使用第3方库,但是源代码只是itertools unique_everseen配方

from collections import Counter
from itertools import repeat, chain
from toolz import unique

lst = ['AM43', 'AM22', 'AM51', 'AM43', 'AM22',
       'AM51', 'AM43', 'AM22', 'AM51']

c = Counter(lst)
uniques = unique(lst)

res = list(chain.from_iterable(repeat(u, c[u]) for u in uniques))

res

['AM43', 'AM43', 'AM43',
 'AM22', 'AM22', 'AM22',
 'AM51', 'AM51', 'AM51']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM