2007年02月03日 星期六 18:19
现在有个比较大的列表(list对象),其中有些元素是重复的,应该如何去除重复的元素呢?谢谢。 -- 从前有一只很冷的毛毛虫,他想获得一点温暖。而获得温暖的机会只有从树上掉下来,落进别人的领口。 片刻的温暖,之后便失去生命。而很多同类却连这片刻的温暖都没有得到就.. 我会得到温暖么?小心翼翼的尝试,却还是会受到伤害。 我愿为那一刻的温暖去拼,可是谁愿意接受? 欢迎访问偶的博客: http://blog.csdn.net/gashero
2007年02月03日 星期六 18:27
On 2/3/07, gashero <harry.python在gmail.com> wrote: > 现在有个比较大的列表(list对象),其中有些元素是重复的,应该如何去除重复的元素呢?谢谢。 > a = list(set(a)) -- I like python! UliPad <>: http://wiki.woodpecker.org.cn/moin/UliPad My Blog: http://www.donews.net/limodou
2007年02月03日 星期六 18:28
from sets import Set as set l = [1,1,2,3] l = list(set(l)) l = [1,2,3] On 2/3/07, gashero <harry.python在gmail.com> wrote: > > 现在有个比较大的列表(list对象),其中有些元素是重复的,应该如何去除重复的元素呢?谢谢。 > > -- > 从前有一只很冷的毛毛虫,他想获得一点温暖。而获得温暖的机会只有从树上掉下来,落进别人的领口。 > 片刻的温暖,之后便失去生命。而很多同类却连这片刻的温暖都没有得到就.. > 我会得到温暖么?小心翼翼的尝试,却还是会受到伤害。 > 我愿为那一刻的温暖去拼,可是谁愿意接受? > > 欢迎访问偶的博客: > http://blog.csdn.net/gashero > _______________________________________________ > python-chinese > Post: send python-chinese在lists.python.cn > Subscribe: send subscribe to python-chinese-request在lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese -- Dreamingk (tc, 天成) email: dreamingk在gmail.com tc在exoweb.net website: http://www.exoweb.net http://python.cn http://django.cn http://zope.cn Exoweb (北京轩辕互动科技有限公司) 北京市朝阳区金台路甜水园东街2号 甜水园商务中心A505 100026 Python, I love this language. -------------- 下一部分 -------------- 一个HTML附件被移除... URL: http://python.cn/pipermail/python-chinese/attachments/20070203/8ae083bc/attachment.htm
2007年02月03日 星期六 18:34
把list的value放到dict里面作为key,value随便给个值,然后把所有的key就OK。 在 07-2-3,gashero<harry.python at gmail.com> 写道: > 现在有个比较大的列表(list对象),其中有些元素是重复的,应该如何去除重复的元素呢?谢谢。 > > -- > 从前有一只很冷的毛毛虫,他想获得一点温暖。而获得温暖的机会只有从树上掉下来,落进别人的领口。 > 片刻的温暖,之后便失去生命。而很多同类却连这片刻的温暖都没有得到就.. > 我会得到温暖么?小心翼翼的尝试,却还是会受到伤害。 > 我愿为那一刻的温暖去拼,可是谁愿意接受? > > 欢迎访问偶的博客: > http://blog.csdn.net/gashero > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese
2007年02月03日 星期六 21:43
l = [1,3,2,4,3,3,2,4] s = [i for i in l if i not in locals()['_[1]']]
2007年02月03日 星期六 21:54
On 2/3/07, Julius F. Huang <fozzold在gmail.com> wrote: > l = [1,3,2,4,3,3,2,4] > s = [i for i in l if i not in locals()['_[1]']] 我的感觉是使用set可能是最快的,不过没有测试过。不过在2.4中,set是使用c写的了,应该非常快。 -- I like python! UliPad <>: http://wiki.woodpecker.org.cn/moin/UliPad My Blog: http://www.donews.net/limodou
2007年02月03日 星期六 22:55
下周一定要测试一下... 在 07-2-3,limodou<limodou at gmail.com> 写道: > On 2/3/07, Julius F. Huang <fozzold at gmail.com> wrote: > > l = [1,3,2,4,3,3,2,4] > > s = [i for i in l if i not in locals()['_[1]']] > > 我的感觉是使用set可能是最快的,不过没有测试过。不过在2.4中,set是使用c写的了,应该非常快。 > > -- > I like python! > UliPad <>: http://wiki.woodpecker.org.cn/moin/UliPad > My Blog: http://www.donews.net/limodou > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese
2007年02月04日 星期日 10:53
In [1]: def testset(n):
...: l = range(n)*2
...: return list(set(l))
...:
In [5]: def testdict(n):
...: l = range(n)*2
...: result = {}
...: [result.__setitem__(i, 1) for i in l]
...: return result.keys()
...:
In [6]: testdict(10)
Out[6]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [7]: testset(10)
Out[7]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [8]: from timeit import Timer
In [9]: tdict = Timer('testdict(100000)', 'from __main__ import testdict')
In [10]: tset = Timer('testset(100000)', 'from __main__ import testset')
In [11]: tdict.timeit(1)
Out[11]: 0.35537421655545604
In [12]: tset.timeit(1)
Out[12]: 0.091743224348346253
In [13]: tdict.timeit(10)
Out[13]: 3.5437735801617265
In [14]: tset.timeit(10)
Out[14]: 0.96231364600808433
In [15]: def testdict2(n):
....: l = range(n)*2
....: result = {}
....: [result.__setitem__(i,1) for i in l if not i in result]
....: return result.keys()
....:
In [21]: testdict2(10)
Out[21]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [16]: tdict2 = Timer('testdict2(100000)', 'from __main__ import
testdict2')
In [17]: tdict2.timeit(1)
Out[17]: 0.22379073317978282
In [18]: tdict.timeit(1)
Out[18]: 0.33202068978039279
In [19]: tdict2.timeit(10)
Out[19]: 2.2887280366638549
In [20]: tdict.timeit(10)
Out[20]: 3.3217556980007146
set 显然比哈希快太多了,testdict2 又比 testdict 快一点点。
Julius F. Huang 的方案就不用比了,肯定是最慢的。
--
http://codeplayer.blogspot.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://python.cn/pipermail/python-chinese/attachments/20070204/40bfa7e4/attachment-0001.html
2007年02月04日 星期日 10:57
如果我每搞错set在2.4里面是用dict实现的,在2.5又重新实现了。 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://python.cn/pipermail/python-chinese/attachments/20070204/59d4f62c/attachment.html
2007年02月04日 星期日 11:30
On 2/4/07, 3751 <lwm3751在gmail.com> wrote: > 如果我每搞错set在2.4里面是用dict实现的,在2.5又重新实现了。 > 不会吧。我记得2.3中有单独的模块,sets,到2.4中变成内置的了。而且改为c写的了。至于是不是dict实现的,不得而知。 -- I like python! UliPad <>: http://wiki.woodpecker.org.cn/moin/UliPad My Blog: http://www.donews.net/limodou
2007年02月04日 星期日 19:42
我也试了, set不是一般的快啊, 至于我那个,嘿嘿,都慢的看不见了。 :) > > set 显然比哈希快太多了,testdict2 又比 testdict 快一点点。 > Julius F. Huang 的方案就不用比了,肯定是最慢的。 >
2007年02月05日 星期一 11:46
Brightman 2007-02-05 ·¢¼þÈË£º Julius F. Huang ·¢ËÍʱ¼ä£º 2007-02-03 21:43:37 ÊÕ¼þÈË£º python-chinese在lists.python.cn ³ËÍ£º Ö÷Ì⣺ Re: [python-chinese]ÈçºÎÈ¥³ýÁбíÖÐÏàͬµÄÔªËØ l = [1,3,2,4,3,3,2,4] s = [i for i in l if i not in locals()['_[1]']] Õâ¸öÊÇʲôÒâ˼£¿ _______________________________________________ python-chinese Post: send python-chinese在lists.python.cn Subscribe: send subscribe to python-chinese-request在lists.python.cn Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn Detail Info: http://python.cn/mailman/listinfo/python-chinese -------------- 下一部分 -------------- Ò»¸öHTML¸½¼þ±»ÒƳý... URL: http://python.cn/pipermail/python-chinese/attachments/20070205/08ff8a6a/attachment.html
2007年02月05日 星期一 14:22
> > l = [1,3,2,4,3,3,2,4] > s = [i for i in l if i not in locals()['_[1]']] > 这个是什么意思? > 这就是两个邮件列表的麻烦之处了,上面有人已经问过了,我复制下 tocer 的回复 参考 http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/204297 -- http://codeplayer.blogspot.com/ -------------- next part -------------- An HTML attachment was scrubbed... URL: http://python.cn/pipermail/python-chinese/attachments/20070205/efef63b6/attachment.html
2007年02月06日 星期二 10:38
对,我也是从aspn上看到的
2007年02月06日 星期二 10:54
#!/usr/bin/python
a = [1,2,3,1,4,2]
b = []
print a
for i in a:
if i in b:
continue
else:
b.append(i)
print b
On 2/6/07, Julius F. Huang <fozzold在gmail.com> wrote:
> 对,我也是从aspn上看到的
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
--
LinuX Power
Zeuux © 2025
京ICP备05028076号