简体   繁体   English

检查该列表是否包含另一个列表中存在的所有类型的元素

[英]Check that list contains the elements of all the types present in another list

I have two Python lists: components and signature . 我有两个Python列表: 组件签名 I want to check whether all the types listed in signature match at least one of the elements in the component list. 我想检查签名中列出的所有类型是否与组件列表中的至少一个元素匹配。

Here, the signature matches the component list, because there is both a string and a float in components : 这里,签名组件列表相匹配 ,因为有两个字符串,并以组件的浮动:

signature = [float, str]
components = [1.0, [], 'hello', 1]

Here, signature does not match components , because there is no list type. 这里签名 组件 不匹配 ,因为没有列表类型。

signature = [float, list]
components = ['apple', 1.0]

How can I express this condition in Python 3? 我怎样才能在Python 3中表达这个条件?

You may use combination of all() and any() with nested generator expression to achieve this. 您可以使用all()any()与嵌套生成器表达式的组合来实现此目的。 Here I am using isinstance() to check for each type in your signature list matches with the object in components list. 这里我使用isinstance()来检查signature列表中的每个type是否与components列表中的对象匹配。 Using this, your custom function will be as: 使用此功能,您的自定义功能将如下所示:

def check_match(signature, components):
    return all(any(isinstance(c, s) for c in components) for s in signature)

Sample Run: 样品运行:

# Example 1: Condition is matched - returns `True`
>>> signature = [str, int]
>>> components = [1, 'hello', []]
>>> check_match(signature, components)
True

# Example 2: Condition is not matched - returns `False`
>>> signature = [float, list]
>>> components = ['apple', 1.0]
>>> check_match(signature, components)
False

Explanation: Above nested generator expression is comprised of two parts. 说明:上面嵌套的生成器表达式由两部分组成。 First part is: 第一部分是:

all(...`any()` call... for s in signature)

Here, I am iterating signature list to get each element s present in it. 在这里,我迭代signature列表,让每一个元素s存在于它。 all() will return True only when all the ...any() call... logic will return True . all()将返回True只有当所有的...any() call...逻辑将返回True Else it will return False . 否则它将返回False

Second is the ...any() call... generator expression as: 第二个是...any() call...生成器表达式为:

any(isinstance(c, s) for c in components)

Here, for each element c in components list, I am checking whether the type of c is s from the external generator comprehension. 在这里,每个元素ccomponents列表中,我检查的类型是否cs从外部产生理解。 If any of the type matches, any(..) will return True . 如果任何类型匹配,则any(..)将返回True If none of c matches the condition, any(...) will return False . 如果c都不匹配条件,则any(...)将返回False

Another approach is to calculate the difference between the set of types used in components and those you have in signature. 另一种方法是计算组件中使用的类型集与签名中的类型集之间的差异。

unique_signatures = set(signature)
components_type = set(map(type, components))

types_not_used = unique_signatures.difference(components_type)

if len(types_not_used)==0:
    print('All types used')
else:
    print('Types not used:', types_not_used)

I believe there are two main advantages with this solution: 我相信这个解决方案有两个主要优点:

  1. More efficient if your components list was long with many duplicate types, as you reduce the number of comparisons 如果您的组件列表很长且有许多重复类型,那么效率会更高,因为您减少了比较次数
  2. How precise do you want to be in matching the class? 你想要在课堂上匹配的精确程度如何? Should subclasses pass the test? 子类应该通过测试吗? For example, isinstance(1, object) is True : is this behavior desirable to you? 例如, isinstance(1, object)True :这种行为是否适合您?

Using the function provided by the (very good) answer of @Moinuddin, you have the following: 使用@Moinuddin(非常好)答案提供的功能,您有以下内容:

check_match([object], [1, 2.0, 'hello'])
Out[20]: True

while my answer would check object versus ['int', 'float', 'str'] finding no match. 而我的回答是检查object与['int','float','str']找不到匹配。

暂无
暂无

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

相关问题 如何检查一个列表是否包含另一个列表的所有元素,包括重复项 - How to check if a list contains all the elements of another list INCLUDING duplicates 检查列表元素是否存在于另一个列表的元素中 - check if element of list is present in elements of another list 检查具有列表值的列的元素是否存在于另一个列表中 - Check whether the elements of a column with list values are present in another list 确定列表中的所有元素是否都存在并且在另一个列表中的顺序相同 - Determine if all elements in a list are present and in the same order in another list 如何检查一个pandas列中列表中的所有元素是否存在于另一个pandas列中 - How to check if all the elements in list in one pandas column are present in another pandas column 如何检查一个列表是否包含另一个列表的 2 个元素? - How do I check if one list contains 2 elements of another list? 如何检查 Python 列表是否包含另一个列表的重复项未忽略的元素 - How to Check if Python List Contains Elements of Another List Duplicates Not Ignored 在 Python 中,如何检查一个数组是否包含另一个数组/列表的所有元素,包括重复项? - In Python, how can I check if an array contains all elements of another array/list, including duplicates? 如何检查 pandas 列中的字符串列表的元素是否存在于另一列中 - How to check if elements of a list of strings in a pandas column are present in another column 如何检查元组或列表中的所有元素是否在另一个中? - How to check if all elements in a tuple or list are in another?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM