[英]compare two tuple lists and then return true/false
lst = [('NOUN', 'chip'), ('NOUN', 'potato'), ('potato', 'chip')]
permute_lst = [('NOUN', 'chip'), ('potato', 'chip'), ('potato', 'bbq'), ('NOUN', 'potato'), ('potato', 'crisp')]
I want to compare these two lists of tuples in a self-defined function to return a list of Booleans. 我想在自定义函数中比较这两个元组列表,以返回布尔值列表。 My current code:
我当前的代码:
def get_tf(lst):
tf_list = []
for lookup in permute_lst:
if set(lst) == set(lookup):
tf_list.append(True)
else:
tf_list.append(False)
return tf_list
The result tf_list=[False, False, False, False, False]
结果
tf_list=[False, False, False, False, False]
My expected result is like: 我的预期结果是:
tf_list = [True, True, False, True, False]
Use a list comprehension that simply checks to see whether each of your permute_list
items is in the reference list: 使用列表
permute_list
,该列表permute_list
仅检查每个permute_list
项目是否在参考列表中:
return [pair in lst for pair in permute_lst]
Output: 输出:
[True, True, False, True, False]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.