过滤列表
我想过滤列表中重复的元素 例如
foo = ['a','b','c','a','b','d','a','d']
我只感兴趣:
['a','b','c','d']
实现这一目标的有效方法是什么? 干杯
I want to filter repeated elements in my list
for instance
foo = ['a','b','c','a','b','d','a','d']
I am only interested with:
['a','b','c','d']
What would be the efficient way to do achieve this ?
Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
列表(
设置
(foo))
如果您使用的是 Python 2.5 或更高版本,但这不会维持顺序。list(
set
(foo))
if you are using Python 2.5 or greater, but that doesn't maintain order.如果您不关心元素顺序,请将 foo 转换为 set 。
Cast foo to a set, if you don't care about element order.
由于列表理解没有保留顺序的答案,因此我建议如下:
也可以写为
取决于
foo
中有多少元素,通过重复哈希,您可能会获得更快的结果查找而不是通过临时列表重复迭代搜索。c not in temp
验证temp
没有项目c
;当项目添加到集合中时,或 True
部分强制将c
发送到输出列表。Since there isn't an order-preserving answer with a list comprehension, I propose the following:
which could also be written as
Depending on how many elements are in
foo
, you might have faster results through repeated hash lookups instead of repeated iterative searches through a temporary list.c not in temp
verifies thattemp
does not have an itemc
; and theor True
part forcesc
to be emitted to the output list when the item is added to the set.这将是从列表中删除重复项并尽可能保留顺序的最直接方法(尽管这里的“顺序”本质上是错误的概念)。
this would be the most straightforward way of removing duplicates from the list and preserving the order as much as possible (even though "order" here is inherently wrong concept).
如果您关心订购的可读方式如下,
根据您对速度、可维护性、空间消耗的要求,您可能会发现上述方式不合适。在这种情况下,请指定您的要求,我们可以尽力做得更好:-)
If you care about order a readable way is the following
Depending on your requirements of speed, maintanability, space consumption, you could find the above unfitting. In that case, specify your requirements and we can try to do better :-)
如果你编写一个函数来执行此操作,我会使用生成器,它只是想在这种情况下使用。
If you write a function to do this i would use a generator, it just wants to be used in this case.
受到 Francesco 的回答的启发,而不是制作我们自己的
filter()
-type 函数,让内置函数为我们做一些工作:用法:
这可能会或可能不会比在纯 Python 中实现所有工作的答案执行得更快或更慢。基准测试并查看。当然,这只有效一次,但它演示了这个概念。当然,理想的解决方案是使用类:
现在我们可以随心所欲地使用它:
我们可能(也可能不会)再次将性能抛到了九霄云外——使用内置函数的好处可以通过类的开销来抵消。我只是觉得这是一个有趣的想法。
Inspired by Francesco's answer, rather than making our own
filter()
-type function, let's make the builtin do some work for us:Usage:
This may or may not perform faster or slower than an answer that implements all of the work in pure Python. Benchmark and see. Of course, this only works once, but it demonstrates the concept. The ideal solution is, of course, to use a class:
Now we can use it as much as we want:
Once again, we may (or may not) have thrown performance out the window - the gains of using a built-in function may be offset by the overhead of a class. I just though it was an interesting idea.
如果您最后需要一个排序列表,这就是您想要的:
This is what you want if you need a sorted list at the end:
你可以做一些丑陋的列表理解黑客。
You could do a sort of ugly list comprehension hack.