Python “如何打印元组”;生的;
我知道我的代码乱七八糟,但我需要得到作业所需的输出。 我没有学到任何关于元组输出的知识,我是说如何操作。。。 但我在互联网上找到的都是元组,比如:('A',10),带一个键和一个值。 据我所知,我有一个有两个值的元组Python “如何打印元组”;生的;,python,python-3.x,tuples,Python,Python 3.x,Tuples,我知道我的代码乱七八糟,但我需要得到作业所需的输出。 我没有学到任何关于元组输出的知识,我是说如何操作。。。 但我在互联网上找到的都是元组,比如:('A',10),带一个键和一个值。 据我所知,我有一个有两个值的元组 name = input("Enter file:") if len(name) < 1 : name = "mbox-short.txt" handle = open(name) raw_dat = list() time = lis
name = input("Enter file:")
if len(name) < 1 : name = "mbox-short.txt"
handle = open(name)
raw_dat = list()
time = list()
hour = list()
dic = dict()
rdy = list()
for line in handle :
if not line.startswith("From") : continue
if line.startswith("From:") : continue
raw_dat.append(line.split())
for item in raw_dat :
item = item[5]
time.append(item)
for data in time :
hour.append(data[0:2])
for hr in hour:
dic[hr] = dic.get(hr, 0) + 1
for tm in sorted(dic.items()) :
print(tm)
期望输出
04 3
06 1
07 1
09 2
10 3
11 6
14 1
15 2
16 4
17 2
18 1
19 1
谢谢。谢谢@quamrana对我的帮助。。。 这是给出的解决方案
name = input("Enter file:")
if len(name) < 1 : name = "mbox-short.txt"
handle = open(name)
raw_dat = list()
time = list()
hour = list()
dic = dict()
for line in handle :
if not line.startswith("From") : continue
if line.startswith("From:") : continue
raw_dat.append(line.split())
for item in raw_dat :
item = item[5]
time.append(item)
for data in time :
hour.append(data[0:2])
for hr in hour:
dic[hr] = dic.get(hr, 0) + 1
for tm in sorted(dic.items()) :
print(tm[0], tm[1])
解决方案(1):
解决方案(2):
您的意思是:“打印(tm[0],tm[1])”吗?非常感谢您。。。很好的答案,简短而完美的解决方案。真的非常感谢。
print(*tm)
在这段代码中,如果不是line.startswith(“From”):如果line.startswith(“From”):继续raw_dat.append(line.split()),你认为raw_dat.append(line.split())
会被执行吗?@JoeFerndz。我不知道你是什么意思。。。起初我认为这是一个骗人的问题。但是我删掉了(#)一行代码,整个事情就不起作用了。所以我想知道你的意思。它得到了执行,并且是代码的一个重要部分(至少对我来说)。那么print(*tm)
?@GrzegorzSkibinski的工作原理与print(tm[0],tm[1])相同。非常感谢。我知道,它只是更简短,更通用。如果你喜欢的话,那就更像“巨蟒”了will@GrzegorzSkibinski没错,看起来更像是我知道自己在做什么。非常感谢。明天,我将看看“*”通常起什么作用。
name = input("Enter file:")
if len(name) < 1 : name = "mbox-short.txt"
handle = open(name)
raw_dat = list()
time = list()
hour = list()
dic = dict()
for line in handle :
if not line.startswith("From") : continue
if line.startswith("From:") : continue
raw_dat.append(line.split())
for item in raw_dat :
item = item[5]
time.append(item)
for data in time :
hour.append(data[0:2])
for hr in hour:
dic[hr] = dic.get(hr, 0) + 1
for tm in sorted(dic.items()) :
print(tm[0], tm[1])
print(tm)
print(tm[0], tm[1])
print(*tm)