列表中包括字典 list= [{'apple':"11.5","orage":"10.9","xigua":"8.0"},{'apple1':"111.5","orage2":"100.9","xigua1":"80.0"}] # 首先从列表中取出一个一个的字典,使用字典.pop(“key”)就能取出k所对应的V了 orage=[item.pop("orage") for item in list if "orage" in item.keys() ] print(orage) #['10.9']
# 以下是等同写法
for item in re_list: if "orage" in list(item.keys()): orage1=item["orage"] print(orage1) #['10.9']
上面简洁写法是2行,而下面的写法需要4行