我在python中有此列表,下面是嵌套列表的列表,想要统计时间戳的出现次数并打印每个时间戳的计数。我该如何在python中做到这一点?
message= [['619833', 'Mon Jun 3 15:49:22 2019'], ['568391', 'Mon Jun 3 15:49:22 2019'], ['578934', 'Sat Jun 1 18:34:12 2019'], ['699425', 'Sat Jun 1 18:34:12 2019'], ['969231', 'Wed May 29 16:18:07 2019']]
预期结果:
['Mon Jun 3 15:49:22 2019', 2 ]
['Sat Jun 1 18:34:12 2019', 2 ]
这里是2。
答案 0 :(得分:1)
这是@ArkistarvhKltzuonstev回复的编辑版本。
messages = [['619833', 'Mon Jun 3 15:49:22 2019'],['568391', 'Mon Jun 3 15:49:22 2019'],['578934', 'Sat Jun 1 18:34:12 2019'],['699425', 'Sat Jun 1 18:34:12 2019'],['969231', 'Wed May 29 16:18:07 2019']]
# Loop through each list
# Create a list called result which formats the data how you requested
result = [(k, sum(1 for i in messages if k in i)) for k in set(m[1] for m in messages)]
# Prints out that result list
print(result)
这是满足您需求的最快版本。
这是具有可重用功能的更完整的“程序”:
# Loop through each list
# Create a list called result which formats the data how you requested
def print_count(arr):
result = [(k, sum(1 for i in arr if k in i)) for k in set(m[1] for m in arr)]
print(result)
# Create a main function
def main():
# Declare a list (like the one OP provided)
messages = [['619833', 'Mon Jun 3 15:49:22 2019'],['568391', 'Mon Jun 3 15:49:22 2019'],['578934', 'Sat Jun 1 18:34:12 2019'],['699425', 'Sat Jun 1 18:34:12 2019'],['969231', 'Wed May 29 16:18:07 2019']]
# Call the function
print_count(messages)
# Call the main function
main()
答案 1 :(得分:0)
您可以从Counter
界面使用collections
类。它会为您提供所有项目的计数,并且可以直接从列表中对其进行初始化。您可以使用列表推导从嵌套列表中选择所需的元素。
from collections import Counter
message = [['619833', 'Mon Jun 3 15:49:22 2019'],
['568391', 'Mon Jun 3 15:49:22 2019'],
['578934', 'Sat Jun 1 18:34:12 2019'],
['699425', 'Sat Jun 1 18:34:12 2019'],
['969231', 'Wed May 29 16:18:07 2019']]
print(Counter([x[1] for x in message]))
这将为您提供以下输出:
Counter({'Mon Jun 3 15:49:22 2019': 2, 'Sat Jun 1 18:34:12 2019': 2, 'Wed May 29 16:18:07 2019': 1})
您可以在柜台上使用most_common
方法来获取您在问题中提到的结构:
>>> print(c.most_common())
[('Mon Jun 3 15:49:22 2019', 2), ('Sat Jun 1 18:34:12 2019', 2), ('Wed May 29 16:18:07 2019', 1)]