944,147 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 1252
  • Python RSS
Dec 29th, 2006
0

list in another list

Expand 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:

Python Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 7
Unverified User
Matt Tacular is offline Offline
187 posts
since Jun 2006
Dec 29th, 2006
0

Re: list in another list

Hi!

I don't know if you would call this easy, but it seems to work:
Python Syntax (Toggle Plain Text)
  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.:
Python Syntax (Toggle Plain Text)
  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:
Python Syntax (Toggle Plain Text)
  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
Reputation Points: 19
Solved Threads: 58
Junior Poster
mawe is offline Offline
133 posts
since Sep 2005
Dec 29th, 2006
0

Re: list in another list

You can use type()
Python Syntax (Toggle Plain Text)
  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.
Reputation Points: 741
Solved Threads: 694
Nearly a Posting Maven
woooee is online now Online
2,314 posts
since Dec 2006
Dec 29th, 2006
0

Re: list in another list

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:

Python Syntax (Toggle Plain Text)
  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?
Python Syntax (Toggle Plain Text)
  1. if sorted(li) == sorted(li2):
  2. do something
Reputation Points: 75
Solved Threads: 44
Junior Poster
ghostdog74 is offline Offline
156 posts
since Apr 2006
Dec 30th, 2006
0

Re: list in another list

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]
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Python Executable (Py2Exe)
Next Thread in Python Forum Timeline: Excel Add-In RegisterXLL





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC