2016-11-14 54 views
0

我正在研究一個函數,我需要在包含關鍵字並返回它們的字典中查找值(只有具有關鍵字的字典)以及他們的鑰匙。我相信我的代碼是正確的。查找字典中包含關鍵字的鍵的值,並返回這些鍵和值

舉例解釋

{'M':[("One",1400,30.0, 20.5,"oil paint","Austria"),("Three",1430,100.0,102.0,"watercolor","France")], 
     'P':[("Eight",1460, 225.0, 200.0, "fresco","Netherlands"),("Six",1465,81.0, 127.1, "tempera", "Netherlands")], 
     'V':[("Four",1661, 148.0, 257.0,"oil paint", "Austria"),("Two",1630, 91.0, 77.0, "oil paint","USA")], 
     'K':[("Five",1922,63.8,48.1,"watercolor","USA"),("Seven",1950,61.0,61.0,"acrylic paint","USA")], 
     'C':[("Ten",1496,365.0,389.0,"tempera","Italy")], 
     'U':[("Nine",1203,182.0, 957.0,"egg tempera","Italy"), ("Twelve",1200,76.2,101.6,"egg tempera","France")] 
     } 

所以,如果我被搜索的關鍵字「水彩」函數應該返回這個

find_keyword(dictionary2(),'watercolor') 

{'M': [('Three', 1430, 100.0, 102.0,  
'watercolor', 'France')], 'K': [('Five',  
1922, 63.8, 48.1, 'watercolor', 'USA')]} 

正如你所看到的功能只是搜索的關鍵字水彩和返回按照它們在字典中出現的順序排列鍵和值。我認爲我的當前代碼必須接近,但它現在給我一個斷言錯誤,並且每次都沒有返回。有誰知道如何解決這一問題?

當前代碼:

def find_keyword(dictionary,theword): 
    keyword = {} 
    for key, record_list in dictionary.items(): 
     for record in record_list: 
      value = record[1] 
      if theword in record: 
       if key in keyword: 
        keyword[key].append(record) 
       else: 
        keyword[key] = [record] 
        return keyword 
+1

「*有*」 的錯誤是遠遠不夠的信息,也可參見[問]。 –

+1

您在'find_keyword(...,keyword)'中使用'keyword'並稍後將其覆蓋在'keyword = {} – furas

+0

@furas對不起,我正在改變一些東西以使我的代碼更加清晰,希望我的編輯只是現在修復它 – n00bprogrammer22

回答

0
  • ,因爲你要在你的dict返回第一個比賽使用一個OrderedDict
  • 你的方法有許多不必要的花裏胡哨。只需重複思考你的dict,搜索每個關鍵字的關鍵字,並返回每個匹配的關鍵值對。

from collections import OrderedDict 
d = {'M':[("One",1400,30.0, 20.5,"oil paint","Austria"),("Three",1430,100.0,102.0,"watercolor","France")], 
    'P':[("Eight",1460, 225.0, 200.0, "fresco","Netherlands"),("Six",1465,81.0, 127.1, "tempera", "Netherlands")], 
    'V':[("Four",1661, 148.0, 257.0,"oil paint", "Austria"),("Two",1630, 91.0, 77.0, "oil paint","USA")], 
    'K':[("Five",1922,63.8,48.1,"watercolor","USA"),("Seven",1950,61.0,61.0,"acrylic paint","USA")], 
    'C':[("Ten",1496,365.0,389.0,"tempera","Italy")], 
    'U':[("Nine",1203,182.0, 957.0,"egg tempera","Italy"), ("Twelve",1200,76.2,101.6,"egg tempera","France")]} 
d = OrderedDict(sorted(d.items(), key=lambda x:x[1], reverse=True)) 

# -------------- ########## ------------ # 

def contains(seq, key): 
    """ 
    Create A helper function to search for a key 
    In a nested sequence of sequences. 
    """ 
    return any(key in el for el in seq) 

def find_key(d, key): 
    """ 
    Iterate thought your `dict`, search each key for your keyword, and 
    return each key value pair that is a match. 
    """ 
    tmp_dict ={} 
    for k, v in d.items(): 
     for tup in v: 
      if key in tup: 
       tmp_dict[k] = tup 
    return tmp_dict 

print(find_key(d,'watercolor')) 

輸出:

{'M': ('Three', 1430, 100.0, 102.0, 'watercolor', 'France'), 
'K': ('Five', 1922, 63.8, 48.1, 'watercolor', 'USA')} 
相關問題