如何从Python列表中删除副本并保持顺序?(复制)

时间:2022-06-28 23:41:20

This question already has an answer here:

这个问题已经有了答案:

Given a list of strings, I want to sort it alphabetically and remove duplicates. I know I can do this:

给定一个字符串列表,我想按字母排序并删除重复的字符串。我知道我能做到:

from sets import Set
[...]
myHash = Set(myList)

but I don't know how to retrieve the list members from the hash in alphabetical order.

但我不知道如何从散列中按字母顺序检索列表成员。

I'm not married to the hash, so any way to accomplish this will work. Also, performance is not an issue, so I'd prefer a solution that is expressed in code clearly to a fast but more opaque one.

我没有嫁给哈希,所以任何方法都可以。此外,性能也不是问题,所以我更喜欢用代码清楚地表示的解决方案,而不是快速但不透明的解决方案。

6 个解决方案

#1


180  

A list can be sorted and deduplicated using built-in functions:

可以使用内置函数对列表进行排序和删除:

myList = sorted(set(myList))
  • set is a built-in function for Python >= 2.3
  • set是Python >= 2.3的内置函数
  • sorted is a built-in function for Python >= 2.4
  • sort是Python >= 2.4的内置函数

#2


11  

If your input is already sorted, then there may be a simpler way to do it:

如果你的输入已经排序,那么可能有更简单的方法:

from operator import itemgetter
from itertools import groupby
unique_list = list(map(itemgetter(0), groupby(yourList)))

#3


4  

If you want to keep order of the original list, just use OrderedDict with None as values.

如果您想保持原始列表的顺序,只需使用没有值的OrderedDict作为值。

In Python2:

在Python2:

    from collections import OrderedDict
    from itertools import izip, repeat

    unique_list = list(OrderedDict(izip(my_list, repeat(None))))

In Python3 it's even simpler:

在Python3里,它甚至更简单:

    from collections import OrderedDict
    from itertools import repeat

    unique_list = list(OrderedDict(zip(my_list, repeat(None))))

If you don't like iterators (zip and repeat) you can use a generator (works both in 2 & 3):

如果您不喜欢迭代器(zip和repeat),您可以使用生成器(在2和3中工作):

    from collections import OrderedDict
    unique_list = list(OrderedDict((element, None) for element in my_list))

#4


2  

If it's clarity you're after, rather than speed, I think this is very clear:

如果你追求的是清晰,而不是速度,我认为这是非常清楚的:

def sortAndUniq(input):
  output = []
  for x in input:
    if x not in output:
      output.append(x)
  output.sort()
  return output

It's O(n^2) though, with the repeated use of not in for each element of the input list.

这是O(n ^ 2)然而,重复使用的输入列表的每个元素。

#5


1  

> but I don't know how to retrieve the list members from the hash in alphabetical order.

>但是我不知道如何按照字母顺序从散列中检索列表成员。

Not really your main question, but for future reference Rod's answer using sorted can be used for traversing a dict's keys in sorted order:

这不是你的主要问题,但对于未来参考Rod的答案使用排序可以用来遍历一个字典的键,按顺序排列:

for key in sorted(my_dict.keys()):
   print key, my_dict[key]
   ...

and also because tuple's are ordered by the first member of the tuple, you can do the same with items:

而且,由于tuple的顺序是由tuple的第一个成员排序的,所以您可以对项目执行同样的操作:

for key, val in sorted(my_dict.items()):
    print key, val
    ...

#6


0  

For the string data

的字符串数据

 output = []

     def uniq(input):
         if input not in output:
            output.append(input)
 print output     

#1


180  

A list can be sorted and deduplicated using built-in functions:

可以使用内置函数对列表进行排序和删除:

myList = sorted(set(myList))
  • set is a built-in function for Python >= 2.3
  • set是Python >= 2.3的内置函数
  • sorted is a built-in function for Python >= 2.4
  • sort是Python >= 2.4的内置函数

#2


11  

If your input is already sorted, then there may be a simpler way to do it:

如果你的输入已经排序,那么可能有更简单的方法:

from operator import itemgetter
from itertools import groupby
unique_list = list(map(itemgetter(0), groupby(yourList)))

#3


4  

If you want to keep order of the original list, just use OrderedDict with None as values.

如果您想保持原始列表的顺序,只需使用没有值的OrderedDict作为值。

In Python2:

在Python2:

    from collections import OrderedDict
    from itertools import izip, repeat

    unique_list = list(OrderedDict(izip(my_list, repeat(None))))

In Python3 it's even simpler:

在Python3里,它甚至更简单:

    from collections import OrderedDict
    from itertools import repeat

    unique_list = list(OrderedDict(zip(my_list, repeat(None))))

If you don't like iterators (zip and repeat) you can use a generator (works both in 2 & 3):

如果您不喜欢迭代器(zip和repeat),您可以使用生成器(在2和3中工作):

    from collections import OrderedDict
    unique_list = list(OrderedDict((element, None) for element in my_list))

#4


2  

If it's clarity you're after, rather than speed, I think this is very clear:

如果你追求的是清晰,而不是速度,我认为这是非常清楚的:

def sortAndUniq(input):
  output = []
  for x in input:
    if x not in output:
      output.append(x)
  output.sort()
  return output

It's O(n^2) though, with the repeated use of not in for each element of the input list.

这是O(n ^ 2)然而,重复使用的输入列表的每个元素。

#5


1  

> but I don't know how to retrieve the list members from the hash in alphabetical order.

>但是我不知道如何按照字母顺序从散列中检索列表成员。

Not really your main question, but for future reference Rod's answer using sorted can be used for traversing a dict's keys in sorted order:

这不是你的主要问题,但对于未来参考Rod的答案使用排序可以用来遍历一个字典的键,按顺序排列:

for key in sorted(my_dict.keys()):
   print key, my_dict[key]
   ...

and also because tuple's are ordered by the first member of the tuple, you can do the same with items:

而且,由于tuple的顺序是由tuple的第一个成员排序的,所以您可以对项目执行同样的操作:

for key, val in sorted(my_dict.items()):
    print key, val
    ...

#6


0  

For the string data

的字符串数据

 output = []

     def uniq(input):
         if input not in output:
            output.append(input)
 print output