0

I'm writing a program to do some statistics on some times. I want the output to look like

11: 4
22: 3
33: 1

instead of

11:4, 22:3, 33:1

because I find it hard to read.

Here is the code I have

#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")
Joel Banks
  • 141
  • 1
  • 11

2 Answers2

0

just go like this:

for i in timesfreq.items():
    print(i)

This gives

(11, 2)
(14, 1)
(17, 1)
(123, 1)

If you want it exactly as you stated:

for a,b in timesfreq.items():
    print("{}: {}".format(a,b))
Christian Sloper
  • 7,440
  • 3
  • 15
  • 28
  • 1
    Perfect solution! I'm still a beginner so sorry if this was an easy question, but this is exactly what I wanted! – Joel Banks Nov 10 '18 at 22:51
0

One way you can do this as a one-liner:

>>> print('\n'.join([str(key) + ': ' + str(val) for key, val in timesfreq.items()]))
123: 1
17: 1
11: 2
14: 1

Here's a good reference for iterating over key/value pairs in a dictionary: Iterating over dictionaries using 'for' loops

Mixolydian
  • 215
  • 1
  • 8