列表中的唯一元素 [英] Unique Elements in a List
问题描述
是否有一种简单的方法可以从列表中获取Unique元素?
例如:
data = [0.1,0.5,0.6,0.4,0.1,0.5 ,0.6,0.9]
我正在寻找的是列表中的唯一元素0.4和0.9及其
索引。
可能类似哈希表的方法!!
我希望在没有不必要的开销的情况下完成这项工作。而且列表
基本上可以是任何字符串,浮点数,整数等等...
或者它已经可以作为列表或数组的attr?
我不记得看到过类似的东西。
我想我需要更清楚!我的意思是独特元素,元素
不重复。所以在上面的列表中,0.4,0.9是唯一的,因为它们只在列表中存在一次。
这不是最美丽的成语,但它有效...
d = {}
表示数据中的k:
尝试:
d [k] + = 1
除外:
d [k] = 1
为k, v in d.items():
如果v == 1:
打印k
su*******@gmail.com 写道:
是否有一种从列表中获取Unique元素的简单方法?
是。
设置数据类型。
查看python 2.4的参考资料。
-
最好的问候
Roie Kerstein
Is there an easy way to grab the Unique elements from a list?
For Example:
data = [0.1,0.5,0.6,0.4,0.1,0.5,0.6,0.9]
what I am looking for is the unique elements 0.4 and 0.9 with their
index from the list.
Probably something like a Hash Table approach!!
I would like to get this done without unnecessary overhead.And the list
could be essentially anything strings,floats,int etc...
Or is it already avaliable as an attr to a list or an array?
I dont remember seeing anything like that.
OK I need to be more clear I guess!Unique Elements I mean, elements
that are non repeating. so in the above list 0.4, 0.9 are unique as
they exist only once in the list.
This is not the most beautiful idiom, but it works...
d = {}
for k in data:
try:
d[k] += 1
except:
d[k] = 1
for k,v in d.items():
if v == 1:
print k
su*******@gmail.com wrote:
Is there an easy way to grab the Unique elements from a list?
Yes.
The set data type.
Look in the reference of python 2.4.
--
Best regards
Roie Kerstein
这篇关于列表中的唯一元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!