remove list elements..

A

Abandoned

Hi..
I have a problem..
list1=[11, 223, 334, 4223...] 1 million element
list2=[22,223,4223,2355...] 500.000 element

I want to difference list1 to list2 but order very importent..

My result must be:
list3=[11,334,...]

I do this use FOR easly but the speed very imported for me. I want to
the fastest method please help me.

I'm sorry my bad english.

King regards..
 
C

chris.monsanto

Hi..
I have a problem..
list1=[11, 223, 334, 4223...] 1 million element
list2=[22,223,4223,2355...] 500.000 element

I want to difference list1 to list2 but order very importent..

My result must be:
list3=[11,334,...]

I do this use FOR easly but the speed very imported for me. I want to
the fastest method please help me.

I'm sorry my bad english.

King regards..

Research the "set" data type. :)
 
C

Carsten Haese

Hi..
I have a problem..
list1=[11, 223, 334, 4223...] 1 million element
list2=[22,223,4223,2355...] 500.000 element

I want to difference list1 to list2 but order very importent..

My result must be:
list3=[11,334,...]

I do this use FOR easly but the speed very imported for me. I want to
the fastest method please help me.

If you can write this as a for/append loop, then you can easily rewrite
it into a list comprehension:

<blank1> = []
for <blank2> in <blank3>:
if <blank4>:
<blank1>.append(<blank2>)

becomes

<blank1> = [<blank2> for <blank2> in <blank3> if <blank4>]

Now all you need to do is fill in the blanks.

As a hint for what exactly to fill in for blank4, remember (or be
advised) that Python has sets.

HTH,
 
H

Hrvoje Niksic

Abandoned said:
I do this use FOR easly but the speed very imported for me. I want
to the fastest method please help me.

Can you post the code snippet that was too slow for you? Are the
lists sorted?
 
J

J. Clifford Dyer

Hi..
I have a problem..
list1=[11, 223, 334, 4223...] 1 million element
list2=[22,223,4223,2355...] 500.000 element

I want to difference list1 to list2 but order very importent..

My result must be:
list3=[11,334,...]

I do this use FOR easly but the speed very imported for me. I want to
the fastest method please help me.

I'm sorry my bad english.

King regards..

If you're using a recent python, you can create a set from list2, and preserve order with list1 like so:

set2 = set(list2)
list3 = [ x for x in list1 if x not in set2 ]

That will speed up your access to list2 (for 500,000 scans) at the low cost of reading through the whole thing once.

You say order is very important, but if your order is known to be strictly increasing (as it appears from your sample code), you can speed up further by doing:

set1 = set(list1)
set2 = set(list2)
list3 = sorted(set1 - set2)


Though to tell the truth, I'm not sure this actually would be faster, since you access each element of list1 just once in the list comprehension version. And I can't be bothered to try it out with timeit.

Cheers,
Cliff
 
S

Steve Holden

Hi..
I have a problem..
list1=[11, 223, 334, 4223...] 1 million element
list2=[22,223,4223,2355...] 500.000 element

I want to difference list1 to list2 but order very importent..

My result must be:
list3=[11,334,...]

I do this use FOR easly but the speed very imported for me. I want to
the fastest method please help me.

I'm sorry my bad english.

King regards..

Research the "set" data type. :)
Probably not a very helpful suggestion given that ordering is stated to
be very important.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden

Sorry, the dog ate my .sigline so I couldn't cat it
 
M

Marc 'BlackJack' Rintsch

Hi..
I have a problem..
list1=[11, 223, 334, 4223...] 1 million element
list2=[22,223,4223,2355...] 500.000 element

I want to difference list1 to list2 but order very importent..

My result must be:
list3=[11,334,...]

I do this use FOR easly but the speed very imported for me. I want to
the fastest method please help me.

Research the "set" data type. :)
Probably not a very helpful suggestion given that ordering is stated to
be very important.

A `set()` can be part of such a solution.

Ciao,
Marc 'BlackJack' Rintsch
 

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,772
Messages
2,569,593
Members
45,112
Latest member
BrentonMcc
Top