关于印刷Python词典的探讨

关于印刷Python词典的探讨,python,json,python-3.x,dictionary,Python,Json,Python 3.x,Dictionary,我有一本Python字典,叫做top_单词 我想打印它的内容,所以我写了这段代码 for word, count in top_words.items(): print(word, count) 它显示类似这样的内容 如何删除每行之前的数字 此函数正在生成top_单词词典 top_word_sequence = 0 top_word_dataset = {} conn = sqlite3.connect('app.db') selector = conn.cursor() qu

我有一本Python字典,叫做top_单词

我想打印它的内容,所以我写了这段代码

for word, count in top_words.items():

    print(word, count)
它显示类似这样的内容

如何删除每行之前的数字

此函数正在生成top_单词词典

top_word_sequence = 0
top_word_dataset = {}

conn = sqlite3.connect('app.db')
selector = conn.cursor()

query = "SELECT word, count FROM top_words WHERE brand_id = ? ORDER BY count desc LIMIT 10"
selector.execute(query,(brand_id,))

top_words = selector.fetchall()

for single_word in top_words:

    top_word_sequence += 1

    word = single_word[0]
    count = single_word[1]

    top_word_dataset[top_word_sequence] = { 'word' : word, \
                                            'count' : count }

    return top_word_dataset

似乎您只需要字典中的值
top\u words

for key, value in top_words.items():
    print(value)
或者,您可以这样做:

for value in top_words.values():
    print(value)
使用以下命令:

for word, count in top_words.items():
    print(word, count)
注意:使用打印(字、计数)会在输出中添加一些其他格式。
希望这能有所帮助。

你没能显示顶级单词的样子,但我打赌关键是排名,而值是字典中的数据。我的意思是,你不能从打印输出中找出它吗?正如Ignacio提到的,
顶级单词的样子是什么?只是
打印(计数)
?我不认为它是字典。如果
top\u words
包含函数的返回值(在那里调用
top\u word\u dataset
),则它是一个字典数组。你想在最上面的单词中为dict添加类似于
的内容:print(dict['word',dict['count'])