为了账号安全,请及时绑定邮箱和手机立即绑定

Python 将列表逐行分隔开

Python 将列表逐行分隔开

当年话下 2021-08-14 13:30:54
我正在编写一个程序来做一些统计。我希望输出看起来像11: 422: 333: 1代替11:4, 22:3, 33:1因为我觉得很难读。这是我的代码#Copy solves here, in seconds, seperated by a comma for each solvetimes = [11.9, 14.2, 17.3, 11.2 , 123.4]#Blank list to copy modified solves intonewtimes = []for time in times:  #Rounds down every time  time = int(time)  #Adds time to new list  newtimes.append(time)#Sorts list lowest to highestnewtimes.sort()#Gets length of new list, which is amount of solvessolves = len(newtimes)#Set longest and shortest solve timeslongestSolve = max(newtimes)shortestSolve = min(newtimes)timesfreq = {i:newtimes.count(i) for i in newtimes}print(newtimes)print(timesfreq)print(solves)print("Your longest solve was " + str(longestSolve) + " seconds and your shortest solve was " + str(shortestSolve) + " seconds")
查看完整描述

2 回答

?
牛魔王的故事

TA贡献1830条经验 获得超3个赞

就这样:


for i in timesfreq.items():

    print(i)

这给


(11, 2)

(14, 1)

(17, 1)

(123, 1)

如果您完全按照您的说明想要它:


for a,b in timesfreq.items():

    print("{}: {}".format(a,b))


查看完整回答
反对 回复 2021-08-14
?
侃侃尔雅

TA贡献1801条经验 获得超16个赞

您可以将其作为单线执行的一种方法:


>>> print('\n'.join([str(key) + ': ' + str(val) for key, val in timesfreq.items()]))

123: 1

17: 1

11: 2

14: 1


查看完整回答
反对 回复 2021-08-14
  • 2 回答
  • 0 关注
  • 205 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信