-1

I have a list of numbers in a file after binning the values to the nearest integers by using the dict,and Counter functions...

from collections import Counter
count_numbers=dict(Counter([round(x) for x in list_numbers])

and the values i got are:-

{1.0: 4, 2.0: 1, 3.0: 6, 4.0: 2, 5.0: 2, 6.0: 2, 7.0: 1, 9.0: 2, 10.0: 2, 12.0: 2, 13.0: 1, 15.0: 2, 16.0: 1, 17.0: 1}

But how can i get it in this form..? 1.0 4 2.0 1 3.0 6 4.0 2 5.0 2 and so on... ie., i need to remove the colons and commas from the list of values.. How can i do tht..??

McGrady
  • 10,869
  • 13
  • 47
  • 69
D.H.N
  • 69
  • 1
  • 9
  • 2
    Just loop over the dict `.items()` and print the key and value for each item: `for key, val in count_numbers.items(): print(key, val, sep=" ")`. –  May 26 '17 at 03:54
  • If the object is a `dictionary`, then it must have `colons` and `commas`. If you want to get the `key` and `value` of the `dictionary`, then you can use `for key, val in count_numbers.items():`. – arnold May 26 '17 at 03:55

4 Answers4

1

You can format the output to look the way you want like this:

from collections import Counter 
count_numbers = dict(Counter([round(x) for x in list_numbers])
for key, value in count_numbers.items():
    print key, value,
    # in python 3 use
    # print('%s %s ' % (key, value), end='')
print
2ps
  • 15,099
  • 2
  • 27
  • 47
0

Im not sure if youre confusing what you got. That looks like a dictionary to me. If you used print() to print that, then all it did was print it in 'key: value' format.

Mauricio
  • 419
  • 4
  • 14
0

count_numbers.keys() will give you the keys of the dictionary
i.e, 1.0,2.0... as a list similarly count_numbers.values() gives the values 4,1,6... in the same order just manipulate the lists to get the format you need

gowtham
  • 326
  • 1
  • 10
0

Output: 1.0 4 2.0 1 3.0 6 4.0 2 5.0 2

dict = {1.0: 4, 2.0: 1, 3.0: 6, 4.0: 2, 5.0: 2, 6.0: 2, 7.0: 1, 9.0: 2, 10.0: 2, 12.0: 2, 13.0: 1, 15.0: 2, 16.0: 1, 17.0: 1}

for k,v in dict.items():
    print k,v
Alpesh Valaki
  • 1,611
  • 1
  • 16
  • 36