删除列表中的重复项 [英] delete duplicates in list

查看:84
本文介绍了删除列表中的重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

之前一定要出现,所以我已经很抱歉要问了一下

快速谷歌搜索没有给我任何答案。


i有一个列表,我想要一个更简单的列表,没有重复项

一个简单但不知何故的设计解决方案将是

< blockquote class =post_quotes>

a = [1,2,2,3]
d = {}。fromgskeys(a)
b = d.keys()
打印b
[1,2,3]


应该有一个更简单或更直观的解决方案,可能有一个清单

理解=


类似于

b = [x代表x而不是b中的x]
print b



[]


虽然不起作用。


感谢您的帮助

chris

解决方案

这个必须在之前出现,所以我已经抱歉,但是一个
快速的谷歌搜索没有给我任何答案。

我有一个列表,我想要一个更简单的列表,没有重复
一个简单但不知何故设计的解决方案将


在python 2.3中,这应该有效:


导入集

l = [1,2, 2,3]

s = sets.Set(l)


集合是一个容器,其中没有任何对象的不变量两次

持有。所以它适合你的需要。


问候,


Diez


> >>> a = [1,2,2,3]

>>> d = {}。fromfromgs(a)
>>> b = d.keys()
>>> print b


[1,2,3]




那个,或者使用Set(python 2.3+)。实际上我似乎记得

The Python CookBook仍然建议建立一个dict作为最快的

解决方案 - 如果你的元素可以被散列。请参阅

http: //aspn.activestate.com/ASPN/Coo...n/Recipe/52560


干杯,


Bernard 。


christof hoeke写道:

...

我有一个列表,我从想要一个没有重复项的简单列表


规范是:


导入集

simplerlist = list(sets。设置(列表))


如果您的破坏顺序没问题,正如您的示例解决方案所示。

但是dict.fromkeys(a).keys ()可能更快。你的断言:

应该有一个更简单或更直观的解决方案,也许有一个列表
comprehension =




不会''对我来说似乎是不言而喻的。列表理解可能是,例如:


[x代表i,x代表枚举(a)如果i == a.index(x)]

它确实具有以下优点:(a)保持秩序和(b)不需要
需要可清洗(甚至不等于可比性!)的元素 - 但是

它具有O(N * N)的非无关紧要的成本,而其他的
约为O(N)。如果你真的想要类似于你的方法:

>>> b = [x代表x而不是b中的x]




你会有,恐怖! - ),做一个循环,所以名字b总是绑定到

到目前为止的结果列表 (在LC中,名称b仅限于末尾):


b = []

for x in a:

如果x不在b中:

b.append(x)


但是,这也是O(N * N)。在更容易或更直观方面,

我怀疑只有后一种解决方案才有资格。

Alex


hello,
this must have come up before, so i am already sorry for asking but a
quick googling did not give me any answer.

i have a list from which i want a simpler list without the duplicates
an easy but somehow contrived solution would be

a = [1, 2, 2, 3]
d = {}.fromkeys(a)
b = d.keys()
print b [1, 2, 3]

there should be an easier or more intuitive solution, maybe with a list
comprehension=

somthing like
b = [x for x in a if x not in b]
print b


[]

does not work though.

thanks for any help
chris

解决方案

Hi,

this must have come up before, so i am already sorry for asking but a
quick googling did not give me any answer.

i have a list from which i want a simpler list without the duplicates
an easy but somehow contrived solution would be



In python 2.3, this should work:

import sets
l = [1,2,2,3]
s = sets.Set(l)

A set is a container for which the invariant that no object is in it twice
holds. So it suits your needs.

Regards,

Diez


> >>> a = [1, 2, 2, 3]

>>> d = {}.fromkeys(a)
>>> b = d.keys()
>>> print b


[1, 2, 3]



That, or using a Set (python 2.3+). Actually I seem to recall that
"The Python CookBook" still advises building a dict as the fastest
solution - if your elements can be hashed. See the details at

http://aspn.activestate.com/ASPN/Coo...n/Recipe/52560

Cheers,

Bernard.


christof hoeke wrote:
...

i have a list from which i want a simpler list without the duplicates
Canonical is:

import sets
simplerlist = list(sets.Set(thelist))

if you''re allright with destroying order, as your example solution suggests.
But dict.fromkeys(a).keys() is probably faster. Your assertion:
there should be an easier or more intuitive solution, maybe with a list
comprehension=



doesn''t seem self-evident to me. A list-comprehension might be, e.g:

[ x for i, x in enumerate(a) if i==a.index(x) ]

and it does have the advantages of (a) keeping order AND (b) not
requiring hashable (nor even inequality-comparable!) elements -- BUT
it has the non-indifferent cost of being O(N*N) while the others
are about O(N). If you really want something similar to your approach:

>>> b = [x for x in a if x not in b]



you''ll have, o horrors!-), to do a loop, so name b is always bound to
"the result list so far" (in the LC, name b is only bound at the end):

b = []
for x in a:
if x not in b:
b.append(x)

However, this is O(N*N) too. In terms of "easier or more intuitive",
I suspect only this latter solution might qualify.
Alex


这篇关于删除列表中的重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆