How to Verify If One List Is a Subset of Another

How can I verify if one list is a subset of another?

Use set.issubset

Example:

a = {1,2}
b = {1,2,3}
a.issubset(b) # True
a = {1,2,4}
b = {1,2,3}
a.issubset(b) # False

The performant function Python provides for this is set.issubset. It does have a few restrictions that make it unclear if it's the answer to your question, however.

A list may contain items multiple times and has a specific order. A set does not. Additionally, sets only work on hashable objects.

Are you asking about subset or subsequence (which means you'll want a string search algorithm)? Will either of the lists be the same for many tests? What are the datatypes contained in the list? And for that matter, does it need to be a list?

Your other post intersect a dict and list made the types clearer and did get a recommendation to use dictionary key views for their set-like functionality. In that case it was known to work because dictionary keys behave like a set (so much so that before we had sets in Python we used dictionaries). One wonders how the issue got less specific in three hours.

How can I check if one list is a subset of the another?

lst1 = [1, 2, 3]
lst2 = [3, 4, 5]

set(lst1).issubset(lst2)
# False
lst1 = [1, 2, 3]
lst2 = [1, 2, 3]
set(lst1).issubset(lst2)
# True

check if a list subset of another list (values can be repetative)

Using Counter

from collections import Counter 

def is_second_list_in_first(first_list, second_list):
' checks if 2nd list of elements is in first list of elemetns with sufficient counts '
# get counts of two lists
count_first = Counter(first_list)
count_second = Counter(second_list)

# check if count of elements exists in first list
return all(count_second[key] <= count_first[key] for key in count_second)

# Tests
print(is_second_list_in_first([1, 4, 1, 3, 5], [1])) # True
print(is_second_list_in_first([1, 4, 1, 3, 5], [1, 1])) # True
print(is_second_list_in_first([1, 4, 1, 3, 5], [1, 1, 1])) # False

How to check if a nested list is a subset of another nested list

You can iterate over list_2 and check whether its elements are contained in list_1:

list_1 = [[[1, 2], [2, 3]], [[3, 4], [5, 6]]]
list_2 = [[[3, 4], [5, 6]]]

all(x in list_1 for x in list_2) # Option 1.
all(map(list_1.__contains__, list_2)) # Option 2.

The second version works for lists (and other types) but a in b is more general since it falls back on b.__iter__ if b.__contains__ is not defined.

Why can't you use 'in' to test if a one list is a subset of another list?

[2,3] `is not in` [1,2,3,4]

[2,3] `is in` [1, [2,3] ,4]

2 and 3 are elements in the list but the list [2,3] is not an element in the list.

How to check if a list is a subset of another list in java?

The approach to solve this problem is very similar to solving substring matching problem.

You start by checking if the first element of the supposedly subset list(henceforth to be refereed to as SSL).

Once you get a match, note the index (henceforth will be referred to as myGuy in the main list where the match is found, proceed to check if the subsequent elements of SSL matches the main list.

If your match is complete then simply return. If not, then you can do one of the two. If the main list has elements left, then increment myGuy and then it becomes the new index from where you start to iterate in the main list.

If no elements are left and still no complete match then it's not a subset.

Here is how you can do it in Java:

private static boolean checkSubList(int[] mainList, int[] subList) {
int currentIteratingPointer;
int matchCounter = 0;

for (int i = 0; i < mainList.length; i++) {
currentIteratingPointer = i;
for (int j = 0; j < subList.length; j++) {
if (mainList[currentIteratingPointer] == subList[j]) {
System.out.println(mainList[currentIteratingPointer]);
++matchCounter;
++currentIteratingPointer;
} else {
--matchCounter;
break;
}
}

i = currentIteratingPointer;
}

return matchCounter == subList.length; // You can count the number of occurance of the sublist if you change
// it to int and return the matchCounter a
}

Check if a list is a subset of another with no repititions python

It seems that you need a multiset (as suggested by your use of set), Python provides it's own multiset implementation through collections.Counter, so use it:

from collections import Counter

short = ['a','a']
large = ['a','b','c']

short_counts = Counter(short)
large_counts = Counter(large)

print(all(value <= large_counts.get(key, 0) for key, value in short_counts.items()))

Output

False

Putting all in a function and adding some examples we get:

def sub_multi_set(sh, la):
short_counts = Counter(sh)
large_counts = Counter(la)
return all(value <= large_counts.get(key, 0) for key, value in short_counts.items())

print(sub_multi_set(['a', 'a'], ['a', 'b', 'c']))
print(sub_multi_set(['a'], ['a', 'b', 'c']))
print(sub_multi_set(['a', 'a'], ['a', 'a', 'a', 'b', 'c']))
print(sub_multi_set(['a', 'b'], ['a', 'b', 'c']))
print(sub_multi_set(['d', 'b'], ['a', 'b', 'c']))

Output

False
True
True
True
False

How to check whether a vector is subset of another one in R?

You can test if length is == and if any values of A have a 1 on positions where B has a 1 and combine the conditions with &&.

length(A) == length(B) && any(A[B==1]==1)
#[1] TRUE

Fulfilling the condition in the original question: A and B have the same length and thus have the same number of elements; yet, A is a subset of B, since A has elements 1 in the same place as B.

To fulfill:

  • A and B have the same length and thus have the same number of elements;
  • A should have at least one 1 at places wherever B has 1
  • A can have only 0s at all places where B has 0s.
length(A) == length(B) && any(A[B==1]==1) && all(A[B==0]==0)

To fulfill:

  • A and B have the same length and thus have the same number of elements;
  • A can have only 0s at all places where B has 0s.
length(A) == length(B) && all(A[B==0]==0)


Related Topics



Leave a reply



Submit