[英]How can I check if one list is a subset of the another?
Here is the current code I have:这是我目前的代码:
a = input('Enter words: ')
b, c = a.split()
q = []
z = []
for i in b:
q.append(i)
for j in c:
z.append(j)
for letters in q:
if letters in z:
print('yes')
It will output 'yes'
if the letter
in q
is also in z
.如果q
的letter
也在z
,它将输出'yes'
。
Is there someway to check if all instances of characters in one list are in another.有没有办法检查一个列表中的所有字符实例是否都在另一个列表中。 Like:喜欢:
for letters in q:
if all letters in z: #all
print('yes')
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
I believe this is what you want:我相信这就是你想要的:
if all(letter in z for letter in q):
print('yes')
Simplified full working code:简化的完整工作代码:
q, z = input('Enter words: ').split()
if all(letter in z for letter in q):
print('yes')
Sample runs:示例运行:
$ python test.py
Enter words: cat tack
yes
$ python test.py
Enter words: cat bat
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.