removing common elemets in a list

S

saif.shakeel

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
 
T

Tim Golden

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
 
G

Gary Herron

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???)





Using a set:
set([1, 2, 3])



Building the list element by element:
.... if e not in r:
.... r.append(e)
....[1, 2, 3]



Using a dictionary:
>>> d = dict(zip(l,l))
>>> d {1: 1, 2: 2, 3: 3}
>>> d.keys() [1, 2, 3]
>>>
 
J

John Zenger

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('c29ydGVkKGxpc3Qoc2V0KHYpKSxrZXk9bGFtYmRhIHg6di5pbmRleCh4KSk='),locals())
 
M

Michael Bentley

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

import base64
def uniq(v):
return
eval(base64.b64decode
('c29ydGVkKGxpc3Qoc2V0KHYpKSxrZXk9bGFtYmRhIHg6di5pbmRleCh4KSk='),local
s())

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top