473,757 Members | 9,463 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Unique Elements in a List

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.

Jul 19 '05
24 4178
aa**@pythoncraf t.com (Aahz) writes:
[x for x in data if data.count(x) == 1]

suffice? it is also "stable" preserving order of items. Lemme demo:


Only for small datasets -- this is an O(N^2) algorithm.


I realized that, but maybe I should've pointed it out too. For the OP if
he/she is unaware - notation O(N^2) (big O n squared) means the computing time
of the algorithm increases exponentially (where exponent is 2) relative to the
size of the input.

Eg. if the input size is i and it takes p seconds to compute it, then given
input size 10*i, the computing time would be 100*p. These notions can apply
for memory usage as well, but the problem in here is the computing time:
list.count() must iterate through the list each time, and as such the loop

[x for x in data if data.count(x) == 1]

iterates through each item in data (x for x in data), and for each item it
will again iterate through each item in data to see how many times it
occurred. If data contains 200 items, this idiom would iterate the structure
40 000 times. With today's computers one wouldn't notice it, unless each item
requires heavy processing (eg. launching a separate process per item
etc). However, if the length of the data can be thousands or even tens of
thousands, this idiom would become unusable. If data contained 75 000 items,
the loop would do 25 625 000 000 iterations, effectively bringing cpu to
halt..

So, generally one should avoid using exponential O(n^k) (where k > 1)
algorithms, unless faster O(n) or O(n*lg(n)) solution is either impossible (or
too complex, and inputs are known to be small etc).

Wikipedia has good resources and pointers to related things, see
http://en.wikipedia.org/wiki/Analysis_of_algorithms

--
#!/usr/bin/perl -w
$h={23,69,28,'6 e',2,64,3,76,7, 20,13,61,8,'4d' ,24,73,10,'6a', 12,'6b',21,68,1 4,
72,16,'2c',17,2 0,9,61,11,61,25 ,74,4,61,1,45,2 9,20,5,72,18,61 ,15,69,20,43,26 ,
69,19,20,6,64,2 7,61,22,72};$_= join'',map{chr hex $h->{$_}}sort{$a<= >$b}
keys%$h;m/(\w).*\s(\w+)/x;$_.=uc substr(crypt(jo in('',60,28,14, 49),join'',
map{lc}($1,subs tr $2,4,1)),2,4)." \n"; print;
Jul 19 '05 #21
Edvard Majakari wrote:
I realized that, but maybe I should've pointed it out too. For the OP if
he/she is unaware - notation O(N^2) (big O n squared) means the computing time
of the algorithm increases exponentially (where exponent is 2) relative to the
size of the input. Normally this is called a polynomial, rather than exponential increase.
Exponential increases are typically of the form (C^N) (they are all
equivalent).
Polynomial times are hallways characterized by their largest exponent,
So you never call something O(N^3 - N^2) Since, as N gets large enough,
The N^2 term shrinks to non-existence.

http://en.wikipedia.org/wiki/Exponential_time
... generally one should avoid using exponential O(n^k) (where k > 1)


Again polynomial, not exponential time. Note that there is no
polynomial time algorithm with (k < 1), since it takes O(n) time
to read the problem.
--Scott David Daniels
Sc***********@A cm.Org

Jul 19 '05 #22
Scott David Daniels <Sc***********@ Acm.Org> writes:
Normally this is called a polynomial, rather than exponential increase.
Exponential increases are typically of the form (C^N) (they are all
equivalent).
Polynomial times are hallways characterized by their largest exponent,
So you never call something O(N^3 - N^2) Since, as N gets large enough,
The N^2 term shrinks to non-existence.


Yup, you are of course, completely correct. I was thinking of "exponent here
is two" and mistakenly named in exponential.

my_text.replace ('exponent','po lynom'), there :)

Reminding of ignoring terms with smaller exponent was good, too.

--
# Edvard Majakari Software Engineer
# PGP PUBLIC KEY available Soli Deo Gloria!

$_ = '45647661726420 4d616a616b61726 92c206120436872 69737469616e20' ; print
join('',map{chr hex}(split/(\w{2})/)),uc substr(crypt(60 281449,'es'),2, 4),"\n";
Jul 19 '05 #23
Scott David Daniels wrote:
Again polynomial, not exponential time. Note that there is no
polynomial time algorithm with (k < 1), since it takes O(n) time
to read the problem.


Being a stickler (I develop software after all :) quantum computers
can do better than that. For example, Grover's algorithm
http://en.wikipedia.org/wiki/Grover%27s_algorithm
for searching an unsorted list solves the problem in O(N**0.5) time.

Being even more picky, I think the largest N that's been tested
so far is on the order of 5.

Andrew
da***@dalkescie ntific.com

Jul 19 '05 #24
> 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 ]


Minor tweak to conserve space:

def bachelor_filter (iter_over_hash ables):
B={}
for index, elem in enumerate(iter_ over_hashables) :
if B.setdefault(el em, index) != index:
B[elem]=None
return [pair for pair in B.items() if pair[1] is not None]

Jul 19 '05 #25

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2303
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:
15
2194
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()
1
381
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 = --
22
2763
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:
5
28389
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!
9
5682
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()
6
10372
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
1
9612
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 -->
5
4014
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
0
9298
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,...
0
10072
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9737
tracyyun
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...
0
8737
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7286
isladogs
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...
0
5172
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...
0
5329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3829
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2698
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.