2012-03-12 124 views
-2

我想爲沒有值的字典指定鍵。所以我想指定日期到字典python指定字典的鍵

這裏我當前的代碼

desc_date = {} 

date = ' '.join(line_of_list[0:2]) 
if desc_date.has_key(date): 
    # Not sure how to assign date to desc_date now 
+0

什麼,確切地說,你打算如何處理字典? – 2012-03-12 22:11:06

+0

我打算將2個鍵和1個值打印出來。 – 2012-03-12 22:22:26

+0

currenty得到這個,但AINT工作的任何幫助,如果desc_ip.has_key(日期): desc_ip [ '一'] = 0 desc_ip [ 'B'] = 0 desc_ip [ '一'] + = 1 打印desc_ip – 2012-03-12 22:23:33

回答

2

你不能。改爲使用set,或者使用「假」值,如None

4

檢查的一個關鍵的存在是非常簡單的:

if date in desc_date: 
    # Yep, the key exists 

你必須使用一個佔位符值,如果你想讓它「看」空:

desc_date[date] = None # Or [] if you'll be storing multiple items per key.