list in another list

Thread Solved

Join Date: Jun 2006
Posts: 187
Reputation: Matt Tacular is an unknown quantity at this point 
Solved Threads: 7
Matt Tacular's Avatar
Matt Tacular Matt Tacular is offline Offline
Unverified User

list in another list

 
0
  #1
Dec 29th, 2006
is there an easy way to detect if the entire contents of one list are in another?

if I have a list like this: li = [1,2]
and I have another which will have random values, I want the program to do something only if every value of li is in the second list.

I need a way to program this:

  1. x = random.randint(0,10)
  2. y = random.randint(0,10)
  3. li = [1,2]
  4. li2 = [x,y]
  5.  
  6.  
  7. if every value of li2 is in li:
  8. do this
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 133
Reputation: mawe is an unknown quantity at this point 
Solved Threads: 58
mawe mawe is offline Offline
Junior Poster

Re: list in another list

 
0
  #2
Dec 29th, 2006
Hi!

I don't know if you would call this easy, but it seems to work:
  1. def is_sublist( lst1, lst2 ):
  2. for elem in lst1:
  3. if elem not in lst2:
  4. return False
  5. return True
e.g.:
  1. l1 = [1,2]
  2. l2 = [2,4,6]
  3. l3 = [1,2,3]
  4. print is_sublist(l1, l2) # --> False
  5. print is_sublist(l1, l3) # --> True
If you use sets instead of lists, there is a builtin function:
  1. s1 = set([1,2])
  2. s2 = set([2,3,4])
  3. s3 = set([1,2,3])
  4. print s1.issubset(s2)
  5. print s2.issubset(s3)
Hope this helps.

Regards, mawe
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,011
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 286
woooee woooee is offline Offline
Veteran Poster

Re: list in another list

 
0
  #3
Dec 29th, 2006
You can use type()
  1. x = random.randint(0,10)
  2. y = random.randint(0,10)
  3. li = [1,2]
  4. li2 = [x,y]
  5.  
  6. test_list = []
  7. if every_value of li2 is in li:
  8. if type(every_value) == type(test_list) :
  9. do this
Note that 'of li2 is in li' will compare entire list to list, not each element, assuming every_value is a list. It is not considered good code practice to use the letters 'el', 'eye', or 'oh' for variable names as they look too much like numbers and are confusing.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 148
Reputation: ghostdog74 is on a distinguished road 
Solved Threads: 40
ghostdog74 ghostdog74 is offline Offline
Junior Poster

Re: list in another list

 
0
  #4
Dec 29th, 2006
Originally Posted by Matt Tacular View Post
is there an easy way to detect if the entire contents of one list are in another?

if I have a list like this: li = [1,2]
and I have another which will have random values, I want the program to do something only if every value of li is in the second list.

I need a way to program this:

  1. x = random.randint(0,10)
  2. y = random.randint(0,10)
  3. li = [1,2]
  4. li2 = [x,y]
  5.  
  6.  
  7. if every value of li2 is in li:
  8. do this

if your lists comparing only numbers, you can sort them first?
  1. if sorted(li) == sorted(li2):
  2. do something
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,019
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 931
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: list in another list

 
0
  #5
Dec 30th, 2006
This will not work because list a and b are not the same length ...
[php]a = [1, 2]
b = [2, 3, 6, 1]
# this will not work here ...
if sorted(a) == sorted(b):
print 'a is in b'
else:
print 'a is not in b'
[/php]
Using set() will work, like mawe said ...
[php]a = [1, 2]
b = [2, 3, 6, 1]
# this will work ...
if set(a).issubset(set(b)):
print 'a is in b'
else:
print 'a is not in b'
[/php]The next approach is pretty universal ...
[php]def is_a_in_b(a, b):
"""returns True if all elements in list a are in list b, else False"""
ax = []
for x in a:
for y in b:
if x == y:
ax.append(y)
return a == ax

print is_a_in_b([1, 2], [2, 3, 6, 1]) # True
print is_a_in_b([1, 2], [1, 3, 7, 9, 4]) # False
[/php]
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC