我做我的功課 但是當我試圖用字典來算的數字,我發現我無法從列表中得到準確值...如何從字典中獲得價值..?
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)
然後我用的打印,看看是什麼'digit_count'和'內容'。
在DIGIT_COUNT:
在內容{ '5':89, '3':203, '7':79, '8':77, '1':500, '9':69 , '2':304, '6':70, '4':140}
:
[ '5', '3', '7',' 8','1','9','2','6','4']
是否可以通過digit_count獲得計數值? 有什麼想法? 數字我想如89,203,79 ...
你問如何獲得'[ '5', '3', '7',...]'從字典' {'5':89,'3':203,'7':79 ...}'? – Anonymous
我不是100%確定你在問什麼,但是你可能想看看如何正確地迭代字典。 – user2027202827
你應該看看在Python中使用'Counter'對象 –