用python打印字典值和键(索引)

用python打印字典值和键(索引),python,Python,我试图编写一个代码,从用户输入一行代码,将其拆分,并将其提供给名为counts的宏伟字典。一切都很好,除非我们向女王陛下要些数据。我希望数据的格式是,首先打印单词,然后在旁边打印重复的次数。下面是我设法编写的代码 counts = dict() print('Enter a line of text:') line = input('') words = line.split() print('Words:', words) print('Counting:') for word in w

我试图编写一个代码,从用户输入一行代码,将其拆分,并将其提供给名为counts的宏伟字典。一切都很好,除非我们向女王陛下要些数据。我希望数据的格式是,首先打印单词,然后在旁边打印重复的次数。下面是我设法编写的代码

counts = dict()
print('Enter a line of text:')
line = input('')

words = line.split()

print('Words:', words)

print('Counting:')
for word in words:
    counts[word]  = counts.get(word,0) + 1
for wording in counts:
    print('trying',counts[wording], '' )
当它执行时,它的输出是不可原谅的

Words: ['You', 'will', 'always', 'only', 'get', 'an', 'indent', 'error', 'if', 'there', 'is', 'actually', 'an', 'indent', 'error.', 'Double', 'check', 'that', 'your', 'final', 'line', 'is', 'indented', 'the', 'same', 'was', 'as', 'the', 'other', 'lines', '--', 'either', 'with', 'spaces', 'or', 'with', 'tabs.', 'Most', 'likely,', 'some', 'of', 'the', 'lines', 'had', 'spaces', '(or', 'tabs)', 'and', 'the', 'other', 'line', 'had', 'tabs', '(or', 'spaces).']
Counting:
trying 1 
trying 1 
trying 1 
trying 1 
trying 1 
trying 2 
trying 2 
trying 1 
trying 1 
trying 1 
trying 2 
trying 1 
trying 1 
trying 1 
trying 1 
trying 1 
trying 1 
trying 1 
trying 2 
它只是打印尝试和重复的次数,没有单词我想它在字典中被称为索引,如果我错了请纠正我

Thankyou

请帮助我,在回答这个问题时,请记住我是python和stack overflow的新手。

在您的代码中没有任何地方尝试打印单词。您希望它如何出现在输出中?如果您想要该单词,请将其放入要打印的内容列表中:

print(wording, counts[wording])
要了解更多信息,请查找包集合,并使用计数器构造

counts = Counter(words)

将为您计算所有字数。

您的代码中没有任何地方尝试打印字数。您希望它如何出现在输出中?如果您想要该单词,请将其放入要打印的内容列表中:

print(wording, counts[wording])
要了解更多信息,请查找包集合,并使用计数器构造

counts = Counter(words)
将为您完成所有字数计算。

您应该使用counts.items按如下方式迭代计数的键和值:

计数=记录 打印“输入一行文字:” 行=输入 words=line.split 打印“单词:”,单词 打印“计数:” 用文字表示: 计数[字]=计数。getword,0+1 对于word,按计数计数。items:注意这一点! printf'trying{word}{count}' 还请注意,打印时可以使用f字符串。

您应该使用counts.items来迭代counts的键和值,如下所示:

计数=记录 打印“输入一行文字:” 行=输入 words=line.split 打印“单词:”,单词 打印“计数:” 用文字表示: 计数[字]=计数。getword,0+1 对于word,按计数计数。items:注意这一点! printf'trying{word}{count}'
还请注意,打印时可以使用f字符串。

我不明白为什么要尝试打印。 试试这个

counts = dict()
print('Enter a line of text:')
line = input('')

words = line.split()

print('Words:', words)

print('Counting:')
for word in words:
    counts[word]  = counts.get(word,0) + 1
for wording in counts:
    print(wording,counts[wording], '' )

我不明白你为什么要尝试打印。 试试这个

counts = dict()
print('Enter a line of text:')
line = input('')

words = line.split()

print('Words:', words)

print('Counting:')
for word in words:
    counts[word]  = counts.get(word,0) + 1
for wording in counts:
    print(wording,counts[wording], '' )

您拥有的代码迭代字典键并仅打印字典中的计数。您可能希望执行以下操作:

for word, count in counts.items():
    print('trying', word, count)
您可能还想使用

from collections defaultdict
counts = defaultdict(lambda: 0)
因此,当添加到字典中时,代码将非常简单

counts[word] += 1

您拥有的代码迭代字典键并仅打印字典中的计数。您可能希望执行以下操作:

for word, count in counts.items():
    print('trying', word, count)
您可能还想使用

from collections defaultdict
counts = defaultdict(lambda: 0)
因此,当添加到字典中时,代码将非常简单

counts[word] += 1

尝试就像是一个检查点,代码能够生存到这一点。尝试就像是一个检查点,代码能够生存到这一点。