简体   繁体   中英

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]

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:

return [pair in lst for pair in permute_lst]

Output:

[True, True, False, True, False]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM