尝试计算小时数,返回汇总的dict(),但没有列表

时间:2019-01-13 14:57:19

标签: python

我正在尝试打开一个文本文件,以hh:mm:ss格式扫描每一行的时间,然后在hh位置列出,计数并排序小时。

到目前为止,我已经能够隔离每行的小时部分,但是在字典中出现了一些错误,因此它可以累计列出条目

name = input("Enter file:")
if len(name) < 1 : name = "mbox-short.txt"

handle = open(name)

d = dict()

for lin in handle :
    if lin.startswith('From ') : #isolates lines w/ emails
        lin = lin.split()
        lin = lin[5]
        lin = lin.split(':')
        hour = lin[0]
        d[hour] = d.get(hour,0) + 1

print(d)

代码返回以下内容:

{'09': 2, '18': 1, '16': 4, '15': 2, '14': 1, '11': 6, '10': 3, '07': 1, '06': 1, '04': 3, '19': 1, '17': 2}

我知道我已经为所有需要的时间创建了一个字典,但是我没有看到如何创建键和值的排序列表。我应该使用什么功能?

所需的输出是如下的排序列表,并且只是对我已经充分组合的数据的操作:

04 3

06 1

07 1

09 2

10 3

11 6

14 1

15 2

16 4

17 2

18 1

19 1

3 个答案:

答案 0 :(得分:0)

l = [(key,d[key]) for key in d]
l.sort(key= lambda x: int(x[0]))
for i in l:
    print(*i)

output


说明:
首先,我列出了包含dict的键值对的元组列表。就像
[(key,value), (key, value), ...]

我根据每个元组中的第一个元素对列表进行了排序,而不是lambda,您还可以使用将元组作为参数并返回第一个参数的helper函数,如下所示:

def helper(x):
    return int(x[0])
l.sort(key= helper)


排序后,我用新行打印了每个元组。(在打印中,我用* i代替了i,即unpacking,以便我们分别打印出元组中的所有元素)

答案 1 :(得分:0)

错误是针对“ if”条件的“ for”循环之后的缩进块:

for lin in handle :

    if lin.startswith('From ') : #isolates lines w/ emails
        lin = lin.split()
        lin = lin[5]
        lin = lin.split(':')
        hour = lin[0]
        d[hour] = d.get(hour,0) + 1
        print(d)

好的,我认为代码块和我想达到的目标存在一些逻辑上的障碍。因此,让我看看是否可以在这里提供帮助。

通常,在开始编码或进行概念初步证明时,最好不要重用变量名,并且要使用唯一的,易读的变量名,以便于调试。

我认为,针对您的用例,最好的数据结构将是列表,而不是字典,这是您要尝试执行的操作:列表,计数和排序。

因此,如果我要为这种方法编写代码块,那就是:

name = input("Enter file:")
if len(name) < 1 : name = "mbox-short.txt"

hour_list = list()

with open(name) as filehandle:
    for line in filehandle:
        if line.startswith("From "):
            line_split = line.split()
            time = line_split[5]
            time_split = time.split(":")
            hour = time_split[0]
            hour_list.append(int(hour))

print(hour_list)
hour_list.sort() ## Sorting the elements in the hour list, it is inplace method
print(hour_list)
print(len(hour_list))

哪个给出以下输出:

Enter file:
[9, 18, 16, 15, 15, 14, 11, 11, 11, 11, 11, 11, 10, 10, 10, 9, 7, 6, 4, 4, 4, 19, 17, 17, 16, 16, 16]
[4, 4, 4, 6, 7, 9, 9, 10, 10, 10, 11, 11, 11, 11, 11, 11, 14, 15, 15, 16, 16, 16, 16, 17, 17, 18, 19]
27

希望这会有所帮助。

答案 2 :(得分:0)

dict = {'09': 2, '18': 1, '16': 4, '15': 2, '14': 1, '11': 6, '10': 3, '07': 1, '06': 1, '04': 3, '19': 1, '17': 2}

代码:

list = sorted([f'{key} {value}' for key, value in dict.items()])
print(*list, sep='\n')

工作方式:

for key, value in dict.items()

为字典中的每个“键”:“值”对,构建一个包含两个字符串:

f'{key} {value}'

...并将其传递到列表-参见方括号[]-这种单行样式称为“列表理解”,这是一种编码概念,为方便起见。然后调用Python内置方法sorted()传递该列表(这是一个可迭代的对象)。

print()方法也接受可迭代对象,但是要将其解包,您需要放置*字符,这称为splat运算符。将未打包元素的分隔符更改为“ \ n”会导致元素之间的换行。

您还可以按照常规样式编写以下代码:

my_list = []
for key, value in dict.items():
    my_list.append(f'{key} {value}')
    # or even more conventional: my_list.append(str(key) + ' ' + str(value))
sorted(my_list)