Skip to main content
 首页 » 编程设计

python之在python中访问字典列表中的元素

2025年05月04日22mate10pro

大家好,我有这样的字典列表:

 dict1 = {Label:----,Chapter:----,Section:----,Massage:----} 
 dict2 = {Label:----,Chapter:----,Section:----,Massage:----} 
 dict3 = {Label:----,Chapter:----,Section:----,Massage:----} 
 List = [dict1 , dict2 , dict3] 

如果我的标签与字典中的标签相同,我首先要列表中的所有字典打印该字典的消息。

我用了这个方法,但是什么也没得到。

def printMassage(List , mylabel): 
    for dicts in List: 
        if (Label.value == mylabel): 
            print( Massage.value) 

请帮帮我!!

请您参考如下方法:

python 中的字典是如何工作的

value = dictionary[key]  # get the `value` of `key` in `dictionary`. 
dictionary[key] = newvalue  # change the content of `key` in `dictionary` to `newvalue`. 

你的例子

def printMassage(List , mylabel): 
    for dict in List: 
        if (dict["Label"] == mylabel): 
            print(dict["Massage"])