[英]find match index group in python
确定在python中搜索哪个索引组的最简单方法是什么
例:
欲望值
Yes, Yes, No
提供的价值
A = No, No, No
B = Yes, No, No
C = Yes, Yes, No
我们想知道哪个索引组匹配A,B或C?
我将在这里疯狂跳入黑暗中,猜测您要做什么。 如果我猜错了,此答案将无用,我将其删除。
您的示例是,如果input
为[Yes, No, Yes]
,则应匹配C
,即C
Yes, Yes, No
。 因此,大概您想知道input
是否具有与A
, B
和C
相同的元素(以任何顺序)。
一种方法是使用collections.Counter
作为多集:
from collections import Counter
A = Counter(('No', 'No', 'No'))
B = Counter(('Yes', 'No', 'No'))
C = Counter(('Yes', 'Yes', 'No'))
input = ['Yes', 'No', 'Yes']
input_multiset = Counter(input)
if input == A:
print('A')
elif input == B:
print('B')
elif input == C:
print('C')
else:
print('no match')
现在,有一些方法可以简化和/或优化此方法,但是最好的方法是重新考虑问题。 A
, B
和C
之间的差异只是每个Yes
值有多少。 所以:
from collections import Counter
abc = {0: 'A', 1: 'B', 2: 'C']
input = ['Yes', 'No', 'Yes']
input_counts = Counter(input)
yes_count = input_counts['Yes']
print(abc.get(yes_count, 'no match'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.