且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

列表中的唯一元素

更新时间:2022-11-29 15:14:34

我想我需要更清楚!我的意思是独特元素,元素

不重复。所以在上面的列表中,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