Robin Robin - 5 months ago 8
Python Question

How to get value from the dict..?

I am doing my homework
but when I tried to use dict to count the numbers, I found that I cannot get exact value from the list...

def get_data(openfile):
totalcount = 0
digit_count = {d:0 for d in '123456789'}
for line in openfile:
line = line.strip()
if int(line[0]) != 0 and line[0].isdigit():
first_digit = line[0]
totalcount += 1
digit_count[first_digit] += 1
print(digit_count)
contents = []
for data in digit_count:
contents.append(data)
print(contents)


Then I used print to see what is in the 'digit_count' and 'contents'.

in the digit_count:


{'5': 89, '3': 203, '7': 79, '8': 77, '1': 500, '9': 69, '2': 304,
'6': 70, '4': 140}


in the content:


['5', '3', '7', '8', '1', '9', '2', '6', '4']


Is that aviliable to get the count numbers from the digit_count..?
any ideas?
The numbers I want to get such as 89,203,79...

Answer

change your code to:

 for data in digit_count:
            contents.append(digit_count[data])
Comments