| | |
list in another list
Thread Solved |
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:
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)
x = random.randint(0,10) y = random.randint(0,10) li = [1,2] li2 = [x,y] if every value of li2 is in li: do this
•
•
Join Date: Sep 2005
Posts: 133
Reputation:
Solved Threads: 58
Hi!
I don't know if you would call this easy, but it seems to work:
e.g.:
If you use sets instead of lists, there is a builtin function:
Hope this helps.
Regards, mawe
I don't know if you would call this easy, but it seems to work:
Python Syntax (Toggle Plain Text)
def is_sublist( lst1, lst2 ): for elem in lst1: if elem not in lst2: return False return True
Python Syntax (Toggle Plain Text)
l1 = [1,2] l2 = [2,4,6] l3 = [1,2,3] print is_sublist(l1, l2) # --> False print is_sublist(l1, l3) # --> True
Python Syntax (Toggle Plain Text)
s1 = set([1,2]) s2 = set([2,3,4]) s3 = set([1,2,3]) print s1.issubset(s2) print s2.issubset(s3)
Regards, mawe
•
•
Join Date: Dec 2006
Posts: 1,011
Reputation:
Solved Threads: 286
You can use type()
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.
Python Syntax (Toggle Plain Text)
x = random.randint(0,10) y = random.randint(0,10) li = [1,2] li2 = [x,y] test_list = [] if every_value of li2 is in li: if type(every_value) == type(test_list) : do this
•
•
Join Date: Apr 2006
Posts: 148
Reputation:
Solved Threads: 40
•
•
•
•
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)
x = random.randint(0,10) y = random.randint(0,10) li = [1,2] li2 = [x,y] if every value of li2 is in li: do this
if your lists comparing only numbers, you can sort them first?
Python Syntax (Toggle Plain Text)
if sorted(li) == sorted(li2): do something
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]
[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!
![]() |
Similar Threads
- DropDown List (VB.NET)
- Help me make a list of database servers! (Database Design)
- How do I print a list of 'Favorites' ?? (Web Browsers)
- List Catagories (C)
- vB Sessions/Online list (PHP)
- Win98SE - clearing out startup list (Windows 95 / 98 / Me)
- A List of Acronyms (Geeks' Lounge)
- Posting List (DaniWeb Community Feedback)
Other Threads in the Python Forum
- Previous Thread: Python Executable (Py2Exe)
- Next Thread: Excel Add-In RegisterXLL
| Thread Tools | Search this Thread |
alarm app beginner cipher cmd cx-freeze data decimals development dictionary directory dynamic error examples feet file float format function generator getvalue gui halp homework http images import input ip itunes java keycontrol leftmouse line linux list lists logging loop maintain maze millimeter module mouse mysqldb number numbers output parsing path port prime programming projects push py2exe pygame pyglet pymailer pyqt python queue random recursion schedule screensaverloopinactive script scrolledtext slicenotation split sqlite ssh string strings sudokusolver table terminal text thread threading time tlapse tuple tutorial ubuntu unicode url urllib urllib2 variable variables ventrilo verify vigenere web webservice wikipedia wx.wizard wxpython xlwt






