Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > removing common elemets in a list

Reply
Thread Tools

removing common elemets in a list

 
 
saif.shakeel@gmail.com
Guest
Posts: n/a
 
      05-16-2007
Hi,
Suppose i have a list v which collects some numbers,how do i
remove the common elements from it ,without using the set() opeartor.
Thanks

 
Reply With Quote
 
 
 
 
Tim Golden
Guest
Posts: n/a
 
      05-16-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Hi,
> Suppose i have a list v which collects some numbers,how do i
> remove the common elements from it ,without using the set() opeartor.


Is this a test? Why don't you want to use the set operator?
Anyway, you can just move things from one list into another
excluding those which are already moved:

<code>
numbers = [1, 2, 3, 3, 4, 4, 5]
unique_numbers = []
for n in numbers:
if n not in unique_numbers:
unique_numbers.append (n)

print unique_numbers
</code>

It won't be the fastest thing you could do, but it
does work. Using a dictionary would speed things up,
but then you're basically implementing a set using
a dictionary.

TJG
 
Reply With Quote
 
 
 
 
Gary Herron
Guest
Posts: n/a
 
      05-16-2007
(E-Mail Removed) wrote:
> Hi,
> Suppose i have a list v which collects some numbers,how do i
> remove the common elements from it ,without using the set() opeartor.
> Thanks
>
>

Several ways, but probably not as efficient as using a set. (And why
don't you want to use a set, one wonders???)



>>> l = [1,2,3,1,2,1]




Using a set:

>>> set(l)

set([1, 2, 3])



Building the list element by element:

>>> for e in l:

.... if e not in r:
.... r.append(e)
....
>>> print r

[1, 2, 3]



Using a dictionary:

>>> d = dict(zip(l,l))
>>> d

{1: 1, 2: 2, 3: 3}
>>> d.keys()

[1, 2, 3]
>>>


 
Reply With Quote
 
Carsten Haese
Guest
Posts: n/a
 
      05-16-2007
On Tue, 2007-05-15 at 23:17 -0700, (E-Mail Removed) wrote:
> Hi,
> Suppose i have a list v which collects some numbers,how do i
> remove the common elements from it ,without using the set() opeartor.
> Thanks


If the list is sorted, you can weed out the duplicates with
itertools.groupby:

>>> import itertools
>>> L = [1,2,3,3,4,4,5]
>>> [k for (k,_) in itertools.groupby(L, lambda x)]

[1, 2, 3, 4, 5]

HTH,

--
Carsten Haese
http://informixdb.sourceforge.net


 
Reply With Quote
 
John Zenger
Guest
Posts: n/a
 
      05-16-2007
On May 16, 2:17 am, (E-Mail Removed) wrote:
> Hi,
> Suppose i have a list v which collects some numbers,how do i
> remove the common elements from it ,without using the set() opeartor.
> Thanks


Submit this as your homework answer -- it will blow your TA's mind!

import base64
def uniq(v):
return
eval(base64.b64decode('c29ydGVkKGxpc3Qoc2V0KHYpKSx rZXk9bGFtYmRhIHg6di5pbmRleCh4KSk='),locals())

 
Reply With Quote
 
Michael Bentley
Guest
Posts: n/a
 
      05-16-2007

On May 16, 2007, at 10:36 AM, John Zenger wrote:

> On May 16, 2:17 am, (E-Mail Removed) wrote:
>> Hi,
>> Suppose i have a list v which collects some numbers,how do i
>> remove the common elements from it ,without using the set() opeartor.
>> Thanks

>
> Submit this as your homework answer -- it will blow your TA's mind!
>
> import base64
> def uniq(v):
> return
> eval(base64.b64decode
> ('c29ydGVkKGxpc3Qoc2V0KHYpKSxrZXk9bGFtYmRhIHg6di5p bmRleCh4KSk='),local
> s())


Nice! But I think he said he couldn't use set()


 
Reply With Quote
 
janislaw
Guest
Posts: n/a
 
      05-17-2007
On May 16, 8:17 am, (E-Mail Removed) wrote:
> Hi,
> Suppose i have a list v which collects some numbers,how do i
> remove the common elements from it ,without using the set() opeartor.
> Thanks


There was a similar thread on polish python newsletter. The following
post displays 4 different approaches and explores timings:

http://groups.google.com/group/pl.co...3618b18e63f3c9

Fortunately, python code is universal.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
java.lang.NoSuchMethodError: wm.common.session.Common.getCustRptListFromMax Denny Java 1 05-01-2008 07:33 AM
Newbie question, schema, complex types and unordered multiple elemets davidjones@myself.com XML 3 10-17-2007 05:52 PM
Removing GPO setting from XP machine after removing from Domain Piet Slaghekke Computer Support 4 01-02-2007 08:58 PM
removing a namespace prefix and removing all attributes not in that same prefix Chris Chiasson XML 6 11-14-2006 05:08 PM



Advertisments