[英]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. 在这里,每个元素c
在components
列表中,我检查的类型是否c
是s
从外部产生理解。 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: 我相信这个解决方案有两个主要优点:
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.