Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从字典列表中筛选和提取元素_Python_Python 3.x_Namedtuple - Fatal编程技术网

Python 从字典列表中筛选和提取元素

Python 从字典列表中筛选和提取元素,python,python-3.x,namedtuple,Python,Python 3.x,Namedtuple,我有一个命名元组列表,如下所示 record = [({ "first_name":"nadbor", "last_name":"drozd", "occupation":"data scientist", "markingAgentUsed":[ { "consumableLabelCode&

我有一个命名元组列表,如下所示

record = [({
   "first_name":"nadbor",
   "last_name":"drozd",
   "occupation":"data scientist",
   "markingAgentUsed":[
      {
         "consumableLabelCode":"L",
          "amountUsed":{
            "amount" : 100,
            "unit": "litre"
          }
      },
     {
         "consumableLabelCode":"Y",
          "amountUsed":{
            "amount" : 300,
            "unit": "mililitre"
          }
      },
     {
         "consumableLabelCode":"0",
          "amountUsed":{
            "amount" : 999,
            "unit": "gallon"
          }
      }
   ]
})]


[{'first_name': 'nadbor', 'last_name': 'drozd', 'occupation': 'data scientist', 'markingAgentUsed': [{'consumableLabelCode': 'L', 'amountUsed': {'amount': 100, 'unit': 'litre'}}, {'consumableLabelCode': 'Y', 'amountUsed': {'amount': 300, 'unit': 'mililitre'}}, {'consumableLabelCode': '0', 'amountUsed': {'amount': 999, 'unit': 'gallon'}}]}]
我想根据LabelCode的输入参数提取值,我想提取两个值amount和unit


例如,如果我通过O,我应该得到999和加仑作为输出。

这是关于理解数据结构的嵌套(不是命名元组,而是字典的元组列表,其中一些键本身就是字典):-/now!这段代码提取了您需要的内容:

record = [({
   "first_name":"nadbor",
   "last_name":"drozd",
   "occupation":"data scientist",
   "markingAgentUsed":[
      {
         "consumableLabelCode":"L",
          "amountUsed":{
            "amount" : 100,
            "unit": "litre"
          }
      },
     {
         "consumableLabelCode":"Y",
          "amountUsed":{
            "amount" : 300,
            "unit": "mililitre"
          }
      },
     {
         "consumableLabelCode":"0",
          "amountUsed":{
            "amount" : 999,
            "unit": "gallon"
          }
      }
   ]
})]

userKey = '0'
for entry in record[0]['markingAgentUsed']:
    if entry['consumableLabelCode'] == userKey:
        print("Amount - {}".format(entry['amountUsed']['amount']) )
        print("Units - {}".format(entry['amountUsed']['unit']) )
    
inp = '0'

for ma in record[0]['markingAgentUsed']:
    if ma['consumableLabelCode'] == inp:
        print(ma['amountUsed']['amount'], ma['amountUsed']['unit'])

这一切都是关于理解数据结构的嵌套(不是命名的元组,而是字典的元组列表和字典列表,其中一些键本身就是字典:-/now!这段代码提取了您需要的内容:

inp = '0'

for ma in record[0]['markingAgentUsed']:
    if ma['consumableLabelCode'] == inp:
        print(ma['amountUsed']['amount'], ma['amountUsed']['unit'])

你能添加定义命名元组的代码吗?看这一行应该是什么还不清楚。你能添加定义命名元组的代码吗?看这一行应该是什么还不清楚。