Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 27th, 2004, 06:10 PM
bquad20 bquad20 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 13 bquad20 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Comparing Lists

I have a question I need some help with about lists.
I have 2 lists (one being generated from a file and the other one from a database) that I need to compare against each other.

Can some one help me:

- compare the lists against each other
- take the differences between the lists and append that information to a 3rd list so I can print it out

Code:
# generated from reading a file
list 1 ['testName', 'test@email.com', '123 street', '/usr/local/bin/myScript.sh', 'None']

# generated from a database table
list 2 ['testName', 'test@email.com', '456 street', '/usr/local/bin/orTest9d.sh', 'None']

# shows the differences between lists -- HELP!
list 3 []
print "differences:", list 3

Reply With Quote
  #2  
Old July 27th, 2004, 08:57 PM
Strike Strike is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2001
Location: Houston, TX
Posts: 383 Strike User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 41 m 27 sec
Reputation Power: 7
Send a message via ICQ to Strike Send a message via AIM to Strike Send a message via Yahoo to Strike
I suggest you use Python 2.3's sets and then just take the difference of each of the two sets (lists can be coerced into sets simply by calling Set() constructor on them)
__________________
Debian - because life's too short for worrying.
Best. (Python.) IRC bot. ever.

Reply With Quote
  #3  
Old July 27th, 2004, 11:24 PM
caroundw5h caroundw5h is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Canada
Posts: 181 caroundw5h User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 55 m 53 sec
Reputation Power: 0
why don't you check out the zip() function in your python document. the
PHP Code:
 not 
operator and the basic lists methods, like append. then write a function that iterates through the contents of the lists and appends to the datalist you want it to.
__________________
"In theory, there is no difference between theory and practice.
But, in practice, there is."


Reply With Quote
  #4  
Old July 28th, 2004, 02:45 AM
DevCoach DevCoach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2004
Location: London, England
Posts: 1,224 DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Week 5 Days 23 h 4 m 39 sec
Reputation Power: 263
If the order of the elements is not important then Strike is right, sets are what you want.

Code:
>>> import sets
>>> list1 = ['testName', 'test@email.com', '123 street', '/usr/local/bin/myScript.sh', 'None']
>>> list2 = ['testName', 'test@email.com', '456 street', '/usr/local/bin/orTest9d.sh', 'None']
>>> 
>>> print sets.Set(list1) ^ sets.Set(list2)
Set(['/usr/local/bin/myScript.sh', '456 street', '/usr/local/bin/orTest9d.sh', '123 street'])
>>> print sets.Set(list1) - sets.Set(list2)
Set(['/usr/local/bin/myScript.sh', '123 street'])
>>> print sets.Set(list1) | sets.Set(list2)
Set(['456 street', '/usr/local/bin/orTest9d.sh', 'None', '/usr/local/bin/myScript.sh', 'testName', '123 street', 'test@email.com'])
>>> print sets.Set(list1) & sets.Set(list2)
Set(['None', 'test@email.com', 'testName'])
>>> 
>>> #convert a set back into a list:
>>> set = sets.Set(list1) ^ sets.Set(list2)
>>> list(set)
['/usr/local/bin/myScript.sh', '456 street', '/usr/local/bin/orTest9d.sh', '123 street']
>>> 


Dave - The Developers' Coach

Last edited by DevCoach : July 28th, 2004 at 02:47 AM.

Reply With Quote
  #5  
Old July 28th, 2004, 02:52 AM
DevCoach DevCoach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2004
Location: London, England
Posts: 1,224 DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Week 5 Days 23 h 4 m 39 sec
Reputation Power: 263
If the order is important, i.e. you only want to compare list1[0] with list2[0], then you can use a zip and compare the entries, as caroundw5h said.

e.g. This will create a list of tuples of the non-matching entries:

Code:
>>> print [ (x, y) for (x, y) in zip(list1, list2) if x != y]
[('123 street', '456 street'), ('/usr/local/bin/myScript.sh', '/usr/local/bin/orTest9d.sh')]
>>> 


Dave - The Developers' Coach

Reply With Quote
  #6  
Old July 28th, 2004, 06:46 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,536 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 3 m 4 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Yet another way to do this, originally written for working with multiple iterators. But still, itll work prefectly in here too.

Code:
#!/usr/bin/env python

def group(*iterators):
    while True:
        yield [iterator.next() for iterator in iterators]
        
if __name__ == '__main__':

    list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    list2 = [1, 2, 3, 4, 4, 6, 7, 8, 9, 11]
    list3 = []

    for item1, item2 in group(iter(list1), iter(list2)):
        if item1 != item2:
            list3.append((item1, item2))
    print list3


Pretty easy to use..

Quote:
>>> def group(*iterators):
... while True:
... yield [iterator.next() for iterator in iterators]
...
>>> l1 = [1, 2, 3,4 , 5, 6, 7, 8, 9]
>>> l2 = [2, 3, 4, 5, 6, 7, 9, 8, 9]
>>> [(x, y) for x, y in group(iter(l1), iter(l2)) if x != y]
[(1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 7), (7, 9)]
>>>


Still, i think Dave has the best sugestion, if order maters to you, use the zip() function!

Mark.
__________________
programming language development: www.netytan.com Hula


Reply With Quote
  #7  
Old July 28th, 2004, 12:55 PM
caroundw5h caroundw5h is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Canada
Posts: 181 caroundw5h User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 55 m 53 sec
Reputation Power: 0
Quote:
Originally Posted by DevCoach
If the order is important, i.e. you only want to compare list1[0] with list2[0], then you can use a zip and compare the entries, as caroundw5h said.

e.g. This will create a list of tuples of the non-matching entries:

Code:
>>> print [ (x, y) for (x, y) in zip(list1, list2) if x != y]
[('123 street', '456 street'), ('/usr/local/bin/myScript.sh', '/usr/local/bin/orTest9d.sh')]
>>> 


Dave - The Developers' Coach


Nice list comprehension. I didn't even think of that. Very pythonic.

Reply With Quote
  #8  
Old July 31st, 2004, 05:15 AM
DevCoach DevCoach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2004
Location: London, England
Posts: 1,224 DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Week 5 Days 23 h 4 m 39 sec
Reputation Power: 263
Quote:
Originally Posted by netytan
Code:
#!/usr/bin/env python

def group(*iterators):
    while True:
        yield [iterator.next() for iterator in iterators]




This functionality is already built into the standard library, as itertools.izip. From the docs:

Quote:
izip( *iterables)

Make an iterator that aggregates elements from each of the iterables. Like zip() except that it returns an iterator instead of a list. Used for lock-step iteration over several iterables at a time. Equivalent to:

Code:
     def izip(*iterables):
         iterables = map(iter, iterables)
         while iterables:
             result = [i.next() for i in iterables]
             yield tuple(result)

Changed in version 2.3.1: When no iterables are specified, returns a zero length iterator instead of raising a TypeError exception.


Of course there are imap and ifilter functions as well. The itertool functions are all written in C, so should be much faster than the Python equivalents.

If you do any functional programming with lists, generators etc, then it is well worth while getting to know the itertools module, since there are a lot of interesting goodies in there.

Dave - The Developers' Coach

Reply With Quote
  #9  
Old July 31st, 2004, 06:24 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,536 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 3 m 4 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Thanks Dave, i never once thought about the itertools module when i wrote it . And then once i had it i didnt bother looking for an alternative .

Mark.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Comparing Lists


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT