简体   繁体   English

检查另一个列表中是否存在任何两个项目

[英]Check if any two items are present within another list

How to check if any two items of a list are within another list? 如何检查列表中的任何两项是否在另一个列表中?

I am trying work out how to test if certain combinations of words appear within a list. 我正在尝试找出如何测试某些单词组合是否出现在列表中的方法。 For example: 例如:

l1 = ['A', 'B', 'C', 'D', 'E', 'F', 'G']

if all(['A', 'C', 'D']) or all(['A', 'D']) or all(['C', 'D'])in l1:
    print('Violation') 

The idea is to determine if either (or both) A and C exist together with D . 这个想法是要确定ACD一起存在。

I have tried the code above, but I am always getting a violation as I assume it is only testing if a single item for any of the check lists are within L1 ? 我已经尝试了上面的代码,但由于总是假设它仅测试是否有任何检查清单的单个项目在L1范围内,所以我总是遇到违规情况。

If you want to check for these subsets, you will have to check each one separately. 如果要检查这些子集,则必须分别检查每个子集。 Your use of all is also improper. 您对all内容的使用也不正确。 all checks whether all elements of the sequence passed to it are "Truthy" . all检查传递给它的序列中的所有元素是否都是“ Truthy”

all(['A', 'B', 'C'])
# True

So your if actually reduces to: 因此,您的if实际上减少到:

if True or True or True in l1:

The expression short circuits to if True , so this is always executed. 该表达式会短路到if True ,因此始终会执行。


I would to this by performing a set.issuperset() check for every subset on df 's columns. 为此,我将对df列上的每个子集执行set.issuperset()检查。

subsets = [{'A', 'C', 'D'}, {'A', 'D'}, {'C', 'D'}]
columns = set(df)
if any(columns.issuperset(s) for s in subsets):
    print('Violation')

I'm not sure that I understand your question exactly... 我不确定我是否完全理解您的问题...

This code checks if ['A', 'D'] and ['C', 'D'] exist in l1. 该代码检查l1中是否存在['A','D'] and ['C','D']。

if all(i in l1 for i in ['A', 'D']) and all(i in l1 for i in ['C', 'D']):
    print('Violation')

If you want or condition, 如果您想要or条件,

if all(i in l1 for i in ['A', 'D']) or all(i in l1 for i in ['C', 'D']):
    print('Violation')

When you say, if all(['A', 'C', 'D']), each of the letters is converted to a True boolean, thus making the all() statement true, and "Violation" will always be printed. 当您说全部(['A','C','D'])时,每个字母都会转换为True布尔值,从而使all()语句为true,并且始终会打印“ Violation” 。 Here's a way to fix that: 这是一种解决方法:

l1 = ['A', 'B', 'C', 'D', 'E', 'F', 'G']
pattern1 = ['A', 'C', 'D']
pattern2 = ['A', 'D']
pattern3 = ['C', 'D']

for pat in [pattern1, pattern2, pattern3]:
    if all(letter in l1 for letter in pat):
        print("Violation")
        break

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查列表中的每个项目是否出现在另一个列表中的任何项目中? - How Can I Check if Every Item in a List Appears Within Any Items in Another List? 检查一个列表中的任何值是否存在于另一个列表中(最快的解决方案) - Check if any value in one list is present in another list (fastest solution) 如何检查列表中是否出现两个列表项的任何组合 - How to check if any combination of two list items occurs in list 如何使用if语句检查列表中的所有三个项目是否都存在于另一个列表中 - How to make an if statement to check if all three items in list is present in another list 如何检查列表中的字典项? 蟒蛇 - How to check dictionary items are present in the list ? PYTHON 检查列表元素是否存在于另一个列表的元素中 - check if element of list is present in elements of another list 如何检查列表项是否在另一个列表中 - How to check if list item is present on another list 检查字符串中是否有任何列表项 - Check if any list items are in a string 提高此搜索的效率以检查此列表中的任何两个数字是否相加? - Improve the efficiency of this search to check if any two numbers in this list sum to another? 检查列表中是否有任何单词出现在字符串python中 - Check if any word in the list present in string python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM