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. 24 4173
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
On Monday 09 May 2005 03:15 pm, su*******@gmail .com wrote: 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.
from sets import Set
data = [0.1,0.5,0.6,0.4 ,0.1,0.5,0.6,0. 9]
[x for x in Set(data) if data.count(x) == 1]
--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095 http://www.jamesstroud.com/
runes: d = {} for k in data: try: d[k] += 1 except: d[k] = 1
for k,v in d.items(): if v == 1: print k
For this problem, if duplicated keys are common enough, this version is
probably the faster.
With this *different* problem, it seems that sometimes the faster
solution is using "in": http://pyalot.blogspot.com/2005/04/d...ary-speed.html
Bye,
Bearophile
In article <11************ **********@z14g 2000cwz.googleg roups.com>,
"su*******@gmai l.com" <su*******@gmai l.com> wrote: 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.
From your comments downthread, it seems you want to find those elements
of the input sequence which occur exactly once, and return not only
these elements, but also their positions.
One reasonable solution might be as follows:
def unique_elts(seq ):
elts = {}
for pos, elt in enumerate(seq):
elts.setdefault (elt, []).append(pos)
return [ (x, p[0]) for (x, p) in elts.iteritems( )
if len(p) == 1 ]
This returns a list of tuples of the form (x, pos), where x is an
element of seq that occurs exactly once, and pos is its index in the
original sequence. This implementation traverses the input sequence
exactly once, and requires storage proportional to the length of the
input.
-M
--
Michael J. Fromberger | Lecturer, Dept. of Computer Science http://www.dartmouth.edu/~sting/ | Dartmouth College, Hanover, NH, USA
In article <11************ **********@f14g 2000cwb.googleg roups.com>,
runes <ru*********@gm ail.com> wrote: 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
Only if "works" does not include "order preserving".
--
Aahz (aa**@pythoncra ft.com) <*> http://www.pythoncraft.com/
"And if that makes me an elitist...I couldn't be happier." --JMS
"su*******@gmai l.com" <su*******@gmai l.com> writes: 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]
Untested: here's an iterator that gives you the index and value,
without reordering. Uses dicts instead of sets for backwards compatibility.
def unique_elements (data):
seen = {}
for n,x in data:
if x not in seen:
seen[x] = 1
yield n,x
You're right. I somehow missed the index part :-o. It's easy to fix
though. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: kevin parks |
last post by:
hi. I've been banging my head against this one a while and have asked
around, and i
am throwing this one out there in the hopes that some one can shed
some light on
what has turned out to be a tough problem for me (though i am getting
closer).
i have been mucking with a lot of data in a dictionary that looks
like:
|
by: les_ander |
last post by:
Hi,
I have many set objects some of which can contain same group of object
while others can be subset of the other. Given a list of sets,
I need to get a list of unique sets such that non of the set is an
subset of another or contain exactly the same members.
Tried to do the following:
s1=set()
s2=set()
s3=set()
|
by: James Stroud |
last post by:
On Monday 09 May 2005 03:15 pm, superprad@gmail.com wrote:
> Is there an easy way to grab the Unique elements from a list?
from sets import Set
data =
--
|
by: Claudio Jolowicz |
last post by:
Is it possible to store unique objects in an STL container?
Suppose an object of class C is unique:
class C
{
public:
C() {}
~C() {}
private:
|
by: Paulers |
last post by:
Hello all,
I have a string array with duplicate elements. I need to create a new
string array containing only the unique elements. Is there an easy way
to do this? I have tried looping through each element but I am having
issues using redim to adjust the new array. Any help or example code
would be greatly appreciated. thanks!
| |
by: Brian Tkatch |
last post by:
I'm looking for a simple way to unique an array of strings. I came up
with this. Does it make sense? Am i missing anything? (Testing seems to
show it to work.)
Public Function Unique(ByVal List() As String) As String()
' Returns the unique values of in array, in an array.
Dim Temp As New
System.Collections.Specialized.StringCollection()
|
by: Tekkaman |
last post by:
I have a list of lists and I want to define an iterator (let's call
that uniter) over all unique elements, in any order. For example,
calling:
sorted(uniter(, , ]))
must return . I tried the following
implementations:
from itertools import chain
|
by: Asko Telinen |
last post by:
Hi all.
I´m a bit newbie writing xml schemas.
Is it possible to define xml element that must have unique attribute
values in same level. For example if i have a xml - document:
<list>
<subsection name="first">
<!-- subsection contents -->
|
by: =?Utf-8?B?UGF1bA==?= |
last post by:
Hi I have a list of type object. The object has an ID as one of the elements
and I would like to create another list that just has objects with unique IDs.
For example
in the list if I have
listofobject.ID = 1
listofobject.ID =1
listofobject.ID = 2
I would like new list with only
listofobject.ID =1
listofobject.ID = 2
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |