簡體   English   中英

在python中找到匹配索引組

[英]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是否具有與ABC相同的元素(以任何順序)。

一種方法是使用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')

現在,有一些方法可以簡化和/或優化此方法,但是最好的方法是重新考慮問題。 ABC之間的差異只是每個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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM