Using 'in' To Test For Part Of One Sublist In Another In Python
Solution 1:
in looks to see if one sublist is an element (not sublist) of another list:
[1,2] in[[1,2],[3,4]]would be True.
[1,2] in [1,2,3]would be False as would:
[1,2] in [1,2]However:
[1,2] == [1,2]
would be True. Depending on what you're actually trying to do, set objects might be useful.
a = [1,2]
b = [1,2,3]
c = [3,2,1]
d = [1,1,1]
e = set(a)
len(e.intersection(b)) == len(a) #Truelen(e.intersection(c)) == len(a) #True -- Order of elements does not matterlen(e.intersection(d)) == len(a) #FalseSolution 2:
Given your example lists:
list_1 = [[1, 2, 9], [4, 5, 8]]
list_2 = [[1, 2, 3], [4, 5, 6], [1, 2, 5]]This works:
print [this[0:2]==that[0:2] for this in list_1 for that in list_2]
[True, False, True, False, True, False]
Or, use a set:
print [thisforthisin list_1 for that in list_2 ifset(this[0:2])<set(that)]
[[1, 2, 9], [1, 2, 9], [4, 5, 8]]
Be aware that a set is without order, so:
>>> set([1,2])==set([2,1])
TrueA typical use of in is with a string:
>>> 'ab'in'cbcbab'TrueOr a single element in a sequence:
>>> 100inrange(1000)
TrueOr an atomic element in a sequence:
>>> (3,3,3) inzip(*[range(10)]*3)
TrueBut over lapping list element do not work:
>>> [1,2] in [0,1,2,3]
False
Unless the elements are the same atomic size:
>>> [1,2] in [0,[1,2],3]
True
But you CAN use a string to compare list a being 'in' list b like so:
>>>defstringy(li): return''.join(map(str,li))...>>>stringy([1,2,9][0:2])
'12'
>>>stringy([1,2,9][0:2]) in stringy([1,2,5])
True
So your original intent MAY be to check to see of item[0:2] appears anywhere in otherItem but in the order of 'item' in your loop. You can use a string like so:
>>> print [thisforthisin list_1 for that in list_2 if stringy(this[0:2]) in stringy(that)]
[[1, 2, 9], [1, 2, 9], [4, 5, 8]]
This is different than the set version since '12'!='21' and '12' not in '21' So if you changed the order of the elements of list_2:
list_1 = [[1, 2, 9], [4, 5, 8]]
list_2 = [[1, 2, 3], [4, 5, 6], [1, 5, 2]]print [this for this in list_1 for that in list_2 if set(this[0:2])<set(that)]
[[1, 2, 9], [1, 2, 9], [4, 5, 8]] # same answer since sets are unordered
print [this for this in list_1 for that in list_2 if stringy(this[0:2]) in stringy(that)]
[[1, 2, 9], [4, 5, 8]] # different answer...
Solution 3:
printset([1,2]).intersection([1,2,3])==set([1,2])
would be True
using set intersection I think you can get what you want
It is important to note that sets are un-ordered collections unique elements
thus set([1,1,2]) == set([1,2])
and so this may not necessarily work for you for all instances
Post a Comment for "Using 'in' To Test For Part Of One Sublist In Another In Python"