473,401 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

removing common elemets in a list

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

May 16 '07 #1
6 4145
sa**********@gmail.com 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
May 16 '07 #2
sa**********@gmail.com 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]
>>>
May 16 '07 #3
On Tue, 2007-05-15 at 23:17 -0700, sa**********@gmail.com 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:x)]
[1, 2, 3, 4, 5]

HTH,

--
Carsten Haese
http://informixdb.sourceforge.net
May 16 '07 #4
On May 16, 2:17 am, saif.shak...@gmail.com 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())

May 16 '07 #5

On May 16, 2007, at 10:36 AM, John Zenger wrote:
On May 16, 2:17 am, saif.shak...@gmail.com 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() ;-)
May 16 '07 #6
On May 16, 8:17 am, saif.shak...@gmail.com 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.

May 17 '07 #7

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

Similar topics

3
by: Guadala Harry | last post by:
In the following line of code, what is the point of including the 'new' keyword? List.Changed -= new ChangedEventHandler(ListChanged); I'm just a bit confused because I thought 'new' was used...
3
by: Jeremy Owens-Boggs | last post by:
We are trying to implement a dual list box selection where you have two list boxes, You highlight items in the right side list box, click a button and this moves those items over to the left hand...
2
by: Howard Kaikow | last post by:
On a multiboot system, I've got: 1. Office XP installed under an OS installed on drive G. 2. Office 2003 and NS .NET 2003 installed under an OS installed on drive J. When booted to J, if I try...
5
by: nuffnough | last post by:
This is python 2.4.3 on WinXP under PythonWin. I have a config file with many blank lines and many other lines that I don't need. read the file in, splitlines to make a list, then run a loop...
10
by: Backwards | last post by:
Hello all, I'll start by explaining what my app does so not to confuss you when i ask my question. ☺ I have a VB.Net 2.0 app that starts a process (process.start ...) and passes a prameter...
17
by: Eric_Dexter | last post by:
def simplecsdtoorc(filename): file = open(filename,"r") alllines = file.read_until("</CsInstruments>") pattern1 = re.compile("</") orcfilename = filename + "orc" for line in alllines: if not...
7
by: =?Utf-8?B?Sm9lbCBNZXJr?= | last post by:
I have created a custom class with both value type members and reference type members. I then have another custom class which inherits from a generic list of my first class. This custom listneeds...
3
m6s
by: m6s | last post by:
Hello to all, I am having trouble removing an item from list, which the item is a list by itself. <code> for self.line in self.filtered: if self.appid: if self.line <> self.appid: ...
6
by: Peter | last post by:
Hi I have a number of arrays of longs, from which I need to find a single array which only contains the values which appear in all the original arrays. For example, I could have the three...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
jinu1996
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.