2017-08-25 55 views
0

我有一个名为的Python字典top_words关于打印Python字典的询问

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

for word, count in top_words.items(): 

    print(word, count) 

它显示这样的东西。

enter image description here

我怎么能各行之前删除的号码?

top_words字典正在通过此功能生成。

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 
+0

您未能显示出什么'top_words'的样子,但我敢打赌,关键是排名,价值是字典中的数据。 –

+1

我的意思是,你不能从印刷输出中弄清楚吗? – n1c9

+0

正如Ignacio所言,“top_words”的外观是什么? – karthikr

回答

0

好像你只是想从字典中的值top_words

for key, value in top_words.items(): 
    print(value) 

或者,你可以做这样的:(仅当您使用Python 2.X)

for value in top_words.values(): 
    print value 
0

使用此

for word, count in top_words.items(): 
    print word, count 

注意:使用打印(字,count)为输出添加了一些其他格式。 希望这有助于。