我做我的功课 但是当我试图用字典来算的数字,我发现我无法从列表中得到准确值...如何从字典中获得价值..?
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'对象 –