猿问

Python 将列表逐行分隔开

我正在编写一个程序来做一些统计。我希望输出看起来像


11: 4

22: 3

33: 1

代替


11:4, 22:3, 33:1

因为我觉得很难读。


这是我的代码


#Copy solves here, in seconds, seperated by a comma for each solve

times = [11.9, 14.2, 17.3, 11.2 , 123.4]

#Blank list to copy modified solves into

newtimes = []

for time in times:

  #Rounds down every time

  time = int(time)

  #Adds time to new list

  newtimes.append(time)

#Sorts list lowest to highest

newtimes.sort()

#Gets length of new list, which is amount of solves

solves = len(newtimes)

#Set longest and shortest solve times

longestSolve = 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")


当年话下
浏览 206回答 2
2回答

牛魔王的故事

就这样: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))

侃侃尔雅

您可以将其作为单线执行的一种方法:>>> print('\n'.join([str(key) + ': ' + str(val) for key, val in timesfreq.items()]))123: 117: 111: 214: 1
随时随地看视频慕课网APP

相关分类

Python
我要回答