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
andB
have the same length and thus have the same number of elements;A
should have at least one1
at places whereverB
has1
A
can have only0
s at all places whereB
has0
s.
length(A) == length(B) && any(A[B==1]==1) && all(A[B==0]==0)
To fulfill:
A
andB
have the same length and thus have the same number of elements;A
can have only0
s at all places whereB
has0
s.
length(A) == length(B) && all(A[B==0]==0)
Related Topics
How to Check Mousebuttonpress Event in Pyqt6
Multiprocessing: Sharing a Large Read-Only Object Between Processes
How to Properly Round-Up Half Float Numbers
Why Do I Need 'B' to Encode a String with Base64
Given a Url to a Text File, What Is the Simplest Way to Read the Contents of the Text File
Assigning to Variable from Parent Function: "Local Variable Referenced Before Assignment"
Large, Persistent Dataframe in Pandas
How to Build a Numpy Array from a Generator
Pandas Dataframe to List of Dictionaries
How to Locate Element of Credit Card Number Using Selenium Python
List Comprehension VS Generator Expression's Weird Timeit Results
How to Convert a Datetime to Date
Isprime Function for Python Language
What's the Deal with Python 3.4, Unicode, Different Languages and Windows
How to Merge a Transparent Png Image with Another Image Using Pil