如何从列表中提取“小时”并按升序排序/打印它们?

时间:2015-04-11 13:33:28

标签: python-3.x

我的问题是“我如何从此列表中提取”小时“并按升序排序并打印出来?

我在网络文本文件中有这些行(100+):

['From', 'john@hotmail.com', 'Fri', 'Jan', '14', '22:16:24', '2012']

我到目前为止:

HMS = words[5].split()

我的变量HMS包含以下内容

['22:16:24']
['22:03:18']
['10:22:18']
['05:44:31']
['05:15:11']

我想在文件中提取小时数及其出现次数,然后按升序排序和打印,如下所示:

Hours  Occurrences
05     2 times
10     1 times
22     2 times

3 个答案:

答案 0 :(得分:1)

您可以使用列表推导和collections.Counter

>>> HMS =[['22:16:24'],
... ['22:03:18'],
... ['10:22:18'],
... ['05:44:31'],
... ['05:15:11']]
>>> from collections import Counter
>>> c=Counter([t[0].split(':')[0] for t in HMS ])
Counter({'22': 2, '05': 2, '10': 1})

要以升序模式打印,您可以使用sorted功能对字典项进行排序:

>>> print 'Hours  Occurrences'+'\n'+'\n'.join('\t'.join((i,'{} times'.format(j))) for i,j in sorted(c.items()))
Hours  Occurrences
05      2 times
10      1 times
22      2 times

答案 1 :(得分:1)

您可以使用计数器来计算出现次数:

lines = [
    ['From', 'john@hotmail.com', 'Fri', 'Jan', '14', '10:10:24', '2012'],
    ['From', 'john@hotmail.com', 'Fri', 'Jan', '14', '22:16:24', '2012'],
    ['From', 'john@hotmail.com', 'Fri', 'Jan', '14', '10:16:24', '2012']
]
from collections import Counter
c = Counter(line[5].split(":")[0] for line in lines)
print("Hours\tOccurrences")
print("\n".join( "%s\t%d times" % item for item in sorted(c.items())))

结果:

Hours   Occurrences
10  2 times
22  1 times

c = Counter(line[5].split(":")[0] for line in lines)从每一行获取第五个元素,它使用:分割它,获取小时部分并将其传递给计数器。

使用"%s\t%d times" % item for item in sorted(c.items()),我们按照小时数排序后,按照计数器中存储的小时数和频率创建我们想要的字符串。

使用"\n".join(...)我们将所有这些字符串与新行连接。

答案 2 :(得分:0)

这可能会有所帮助

data = [['From', 'john@hotmail.com', 'Fri', 'Jan', '14', '22:16:24', '2012'],
 ['From', 'john@hotmail.com', 'Fri', 'Jan', '14', '23:16:24', '2012'],
 ['From', 'john@hotmail.com', 'Fri', 'Jan', '14', '21:16:24', '2012'],
 ['From', 'john@hotmail.com', 'Fri', 'Jan', '14', '22:02:24', '2012']
]

hour_frequency_list = {}

for temp in data:
  hour = temp[5].split(":")[0]
  if hour in hour_frequency_list:
     hour_frequency_list[hour] += 1
  else:
     hour_frequency_list[hour] = 1

print (hour_frequency_list)  # this can be changed to any format you need eg: display as a table columns or key value pairs

输出

{'21': 1,   '22': 2,   '23': 1}