Getting a list of values from a list of dicts

前端 未结 9 735
执念已碎
执念已碎 2020-11-22 15:22

I have a list of dicts like this:

[{\'value\': \'apple\', \'blah\': 2}, 
 {\'value\': \'banana\', \'blah\': 3} , 
 {\'value\': \'cars\', \'blah\': 4}]
         


        
9条回答
  •  无人共我
    2020-11-22 15:49

    Get key values from list of dictionaries in python?

    1. Get key values from list of dictionaries in python?

    Ex:

    data = 
    [{'obj1':[{'cpu_percentage':'15%','ram':3,'memory_percentage':'66%'}]},
    {'obj2': [{'cpu_percentage':'0','ram':4,'memory_percentage':'35%'}]}]
    

    for d in data:

      for key,value in d.items(): 
    
          z ={key: {'cpu_percentage': d['cpu_percentage'],'memory_percentage': d['memory_percentage']} for d in value} 
          print(z)
    

    Output:

    {'obj1': {'cpu_percentage': '15%', 'memory_percentage': '66%'}}
    {'obj2': {'cpu_percentage': '0', 'memory_percentage': '35%'}}
    

提交回复
热议问题