从列表中删除重复项 [英] Removing duplicates from a list

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

问题描述

我有一个重复成员的列表,我需要让每个条目

唯一。


我想出了两种方法这样做,我想要一些关于

将被视为更多pythonic(或至少是最佳实践)的输入。


方法1(传统方法)


如果是mylist.count(x)> 1:

mylist.remove(x)


方法2(不那么传统)


mylist = set (mylist)

mylist = list(mylist)


转换为集合会删除所有重复项并转换回

列表,好吧,让它回到我想要的列表。


我无法想象一个比另一个快得多的除外在案件

一个巨大的列表,我的'通常只有不到1000元b $ b元素。


您怎么看?


干杯,


罗宾

I''ve a list with duplicate members and I need to make each entry
unique.

I''ve come up with two ways of doing it and I''d like some input on what
would be considered more pythonic (or at least best practice).

Method 1 (the traditional approach)

for x in mylist:
if mylist.count(x) > 1:
mylist.remove(x)

Method 2 (not so traditional)

mylist = set(mylist)
mylist = list(mylist)

Converting to a set drops all the duplicates and converting back to a
list, well, gets it back to a list which is what I want.

I can''t imagine one being much faster than the other except in the case
of a huge list and mine''s going to typically have less than 1000
elements.

What do you think?

Cheers,

Robin

推荐答案

周三,2005年9月14日04:38:35 -0700 schrieb Rubinho:
Am Wed, 14 Sep 2005 04:38:35 -0700 schrieb Rubinho:
我有一个重复成员的列表,我需要让每个条目都是唯一的。

我想出了两种方法,我想要了解更多pythonic(或至少是最佳实践)的内容。
mylist = set(mylist)
mylist = list(mylist)

转换为一个集合会丢弃所有重复项并转换回
列表,好吧,获取它回到了我想要的清单。

我无法想象一个人比其他人快得多,除非在一个巨大的清单和我的去的情况下通常只有不到1000个元素。

你怎么看?
I''ve a list with duplicate members and I need to make each entry
unique.

I''ve come up with two ways of doing it and I''d like some input on what
would be considered more pythonic (or at least best practice). mylist = set(mylist)
mylist = list(mylist)

Converting to a set drops all the duplicates and converting back to a
list, well, gets it back to a list which is what I want.

I can''t imagine one being much faster than the other except in the case
of a huge list and mine''s going to typically have less than 1000
elements.

What do you think?






我会使用设置:


mylist = list(set(mylist))


Thomas


-

ThomasGüttler, http://www.thomas-guettler.de/

电子邮箱:guettli(*)thomas-guettler + de

垃圾邮件捕手:< a href =mailto:ni ************** @ thomas-guettler.de> ni ************** @ thomas-guettler.de



Hi,

I would use "set":

mylist=list(set(mylist))

Thomas

--
Thomas Güttler, http://www.thomas-guettler.de/
E-Mail: guettli (*) thomas-guettler + de
Spam Catcher: ni**************@thomas-guettler.de


Rubinho写道:
Rubinho wrote:
我有一个列表与d我需要让每个参赛作品独一无二。

我想出了两种方法,我想对
会有什么意见。被认为是更加pythonic(或至少是最佳实践)。

方法1(传统方法)

对于m in mylist:
如果是mylist.count(x )> 1:
mylist.remove(x)

方法2(不那么传统)

mylist = set(mylist)
mylist = list(mylist)转换成一个集合会删除所有重复项并转换回
列表,然后将其恢复到我想要的列表中。

我无法想象一个人比另一个人快得多,除非在一个巨大的列表中,我的'通常只有不到1000个元素。
I''ve a list with duplicate members and I need to make each entry
unique.

I''ve come up with two ways of doing it and I''d like some input on what
would be considered more pythonic (or at least best practice).

Method 1 (the traditional approach)

for x in mylist:
if mylist.count(x) > 1:
mylist.remove(x)

Method 2 (not so traditional)

mylist = set(mylist)
mylist = list(mylist)

Converting to a set drops all the duplicates and converting back to a
list, well, gets it back to a list which is what I want.

I can''t imagine one being much faster than the other except in the case
of a huge list and mine''s going to typically have less than 1000
elements.



我会想象2会快得多。方法1使用

''count'',它必须通过列表中的每个元素,其中
将比set set的有效散列慢。我也没有

确保在迭代时移除元素,我认为这是禁止的。


将McGugan

-
http://www.willmcgugan.com
"" .join({''*'':''@'',''^'':''。''}。get(c,0)或chr(97+( ord(c)-84)%26)for c in

" jvyy * jvyyzpthtna ^ pbz")



I would imagine that 2 would be significantly faster. Method 1 uses
''count'' which must make a pass through every element of the list, which
would be slower than the efficient hashing that set does. I''m also not
sure about removing an element whilst iterating, I think thats a no-no.

Will McGugan
--
http://www.willmcgugan.com
"".join({''*'':''@'',''^'':''.''}.get(c,0) or chr(97+(ord(c)-84)%26) for c in
"jvyy*jvyyzpthtna^pbz")


Rubinho写道:
Rubinho wrote:
我有一个重复成员的列表,我需要让每个条目都独一无二。

我想出了两种方法这样做,我想要一些关于
将被视为更多pythonic(或至少是最佳实践)的意见。

方法1(传统方法)

for m in mylist:
if * mylist.count(x)*> * 1:
mylist.remove(x)
I''ve a list with duplicate members and I need to make each entry
unique.

I''ve come up with two ways of doing it and I''d like some input on what
would be considered more pythonic (or at least best practice).

Method 1 (the traditional approach)

for x in mylist:
if*mylist.count(x)*>*1:
mylist.remove(x)




这将是一个奇怪的传统:



That would be an odd tradition:

mylist = [1,2,1,3,2,3]
对于m in mylist:
... 。如果mylist.count(x)> 1:

.... mylist.remove(x)

.... mylist
mylist = [1, 2, 1, 3, 2, 3]
for x in mylist: .... if mylist.count(x) > 1:
.... mylist.remove(x)
.... mylist



[2,1,2,3]#oops!


参见对变异对象进行迭代的意外行为
http://mail.python.org/pipermail/pyt...er/ 298993.html

线程获取最新解释。


相反,Python中算法问题的传统方法是

问Tim Peters,看看他的食谱
http://aspn.activestate.com/ASPN/Coo.../Recipe/52560/

(早于Python的集合类)。


彼得


[2, 1, 2, 3] # oops!

See "Unexpected Behavior Iterating over a Mutating Object"
http://mail.python.org/pipermail/pyt...er/298993.html
thread for the most recent explanation.

Rather, the traditional approach for an algorithmic problem in Python is to
ask Tim Peters, see his recipe at
http://aspn.activestate.com/ASPN/Coo.../Recipe/52560/
(which predates Python''s set class).

Peter


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

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