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

New Free Tools on Dev Shed!

#1
September 20th, 2004, 09:25 AM
 Boceifus
Contributing User

Join Date: Jan 2004
Posts: 103
Time spent in forums: 3 Days 1 h 17 m 20 sec
Reputation Power: 10
Comparing elements of lists

greets,

is it possible to compare elements of one list,against elements of another list?for example:

light=['white', 'grey', 'silver']
dark=['black', 'blue', 'brown']
both=light+dark

test1=['white', 'black']
test2=['white']
test3=['black']

>>light in both
false

>> test1 in both
false

>> test2 and test3 in both
true

i am wondering,why it returns false when i check if "test1" is in the list of "both",but it returns true when i check if "test2" and "test3" are in the list "both"?Both elements of "test1" are in the "both" list,but it returns false,why?can anyone please explain?eventually i would like to be able to take the elements of a list,and see if any of those elements are in another list.
__________________
It is not important if the glass is half full or half empty.What is important,is who has been drinking from MY glass?!?!?

Last edited by Boceifus : September 20th, 2004 at 01:40 PM. Reason: fixed typo :)

#2
September 20th, 2004, 11:44 AM
Registered User

Join Date: Feb 2004
Location: Germany
Posts: 11
Time spent in forums: 4 h 17 m 25 sec
Reputation Power: 0
hi,
first of all, what is "black" in line 3? i think you mean "dark"!?
i always get "false" (changed line 3)
Code:
```light = ['white', 'grey', 'silver']
dark = ['black', 'blue', 'brown']
both= light + dark

test1=['white', 'black']
test2=['white']
test3=['black']

if light in both:
print "true"
else:
print "false"

if test1 in both:
print "true"
else:
print "false"

if test2 and test3 in both:
print "true"
else:
print "false"```

perhaps you should take a look here, where list comparison was already discussed

#3
September 20th, 2004, 01:56 PM
 Boceifus
Contributing User

Join Date: Jan 2004
Posts: 103
Time spent in forums: 3 Days 1 h 17 m 20 sec
Reputation Power: 10
Quote:
 Originally Posted by Naddel hi, first of all, what is "black" in line 3? i think you mean "dark"!? perhaps you should take a look here, where list comparison was already discussed

hehe,yes i meant to put "both=light+dark",thank you.and i did come upon that thread when i did a search,but that finds the differences.

what i want,is to check if the elements of a given list(test1), to see if one or more of those elements exsist in another list(the "both" list).then i will need to print out only those elements that match,and not the entire contents of either list.is this possible?

#4
September 20th, 2004, 02:39 PM
Registered User

Join Date: Feb 2004
Location: Germany
Posts: 11
Time spent in forums: 4 h 17 m 25 sec
Reputation Power: 0
yeah, check out DevCoach's post again
Code:
```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)```
prints
"Set(['None', 'test@email.com', 'testName'])"

if you want it without the "Set" and brackets, use the following form:
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']

result = sets.Set(list1) & sets.Set(list2)
for i in result:
print i```

for your problem (short example ):
Code:
```import sets

light = ['white', 'grey', 'silver']
test1 = ['white', 'black']

result = sets.Set(light) & sets.Set(test1)
for i in result:
print i```

hope this is for your problem, otherwise i am to stupid

#5
September 20th, 2004, 02:42 PM
 DevCoach
Contributing User

Join Date: Feb 2004
Location: London, England
Posts: 1,585
Time spent in forums: 2 Weeks 4 Days 2 h 58 m 23 sec
Reputation Power: 1372
You are searching for a list inside another list which will always be false since the list only has strings in it. e.g.

Code:
```>>> ['foo'] in ['foo', 'bar', 'baz']
False
>>> 'foo' in ['foo', 'bar', 'baz']
True
>>> ['foo'] in [ ['foo'], 'bar', 'baz']
True```

Take a look at the sets module. This will do what you really want.

Dave - The Developers' Coach

#6
September 21st, 2004, 04:24 AM
 Boceifus
Contributing User

Join Date: Jan 2004
Posts: 103
Time spent in forums: 3 Days 1 h 17 m 20 sec
Reputation Power: 10
Sorry took me so long to reply,but once I got your suggestions,I was happily playing around with sets.Sets are excatly what I wanted,thank you.

Quote:
 Originally Posted by Naddel for your problem (short example ): Code: ```import sets light = ['white', 'grey', 'silver'] test1 = ['white', 'black'] result = sets.Set(light) & sets.Set(test1) for i in result: print i``` hope this is for your problem, otherwise i am to stupid

#7
October 5th, 2004, 01:35 PM
 inkedmn
Tattooed Python-Lovin' Freak-Boy

Join Date: Dec 2001
Location: orange county, CA
Posts: 16
Time spent in forums: < 1 sec
Reputation Power: 0
check out issubset() and issuperset in the sets.Set class

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > Comparing elements of lists