2017-08-25 74 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)爲輸出添加了一些其他格式。 希望這有助於。