我只需要打印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
演示