获取 Counter 最常见的键值
Get Counter most common key value
我只需要打印 most_common(1)
返回的最常见元组的键,但它返回的是一个元组。我怎样才能得到钥匙?
对于给定的示例,它应该只打印 The System
,现在我得到 ('The System', 3)
。我无法在文档中找到可以做到这一点的功能。
from collections import Counter
def main():
cmp_sub_list = ['System', 'System', 'The System', 'Customer', 'The System', 'The System']
most_common_subject = Counter(cmp_sub_list).most_common(1)
print(most_common_subject)
if __name__ == '__main__':
main()
您可以访问元组索引,即:
most_common_subject = Counter(cmp_sub_list).most_common(1)[0][0]
# The System
我只需要打印 most_common(1)
返回的最常见元组的键,但它返回的是一个元组。我怎样才能得到钥匙?
对于给定的示例,它应该只打印 The System
,现在我得到 ('The System', 3)
。我无法在文档中找到可以做到这一点的功能。
from collections import Counter
def main():
cmp_sub_list = ['System', 'System', 'The System', 'Customer', 'The System', 'The System']
most_common_subject = Counter(cmp_sub_list).most_common(1)
print(most_common_subject)
if __name__ == '__main__':
main()
您可以访问元组索引,即:
most_common_subject = Counter(cmp_sub_list).most_common(1)[0][0]
# The System