简体   繁体   中英

python: a list comprehension over a list of dictionaries of list of dictionary items

I know this is a worst title but let me explain the question by sample. My data is:

data = [
  {
    "subdata": [ # subdata various number of dictionaries of same keys, including "ext_id", ...
      {
        "ext_id": "12345",   # ... but of different values
        ...
      },
      {
        "ext_id": "54321",
        ...
      }
    ],
    ...
  },
  ... # and many other dictionary items with "subdata", which in turn contains 
      # a list of dictionaries containing "ext_id" and corresponding values
]

my goal is make a list of the pair of "ext_id"s in "subdata", ie

goal = [
  ("12345", "54321"),
  (...)
]

I know a for-loop is okay for this goal, but wondering if a list comprehension is possible? I tried this: goal = [x["ext_id"] for y in data for x in y["subdata"]] and get a flattened version of goal ["12345", "54321", ...] rather than a 2-D list.

Any advices are appreciated.

If you have a data structure like this:

data = [
  {
    "subdata": [
      {
        "ext_id": "12345",
      },
      {
        "ext_id": "54321",
      }
    ],
  },
  {
    "subdata": [
      {
        "ext_id": "98765",
      },
      {
        "ext_id": "56789",
      }
    ],
  }
]

Then to get the output that you want, you could use list comprehension (and a generator comprehension too) as follows:

goal = [tuple(dict_['ext_id'] for dict_ in subdata['subdata']) for subdata in data ]

goal will contain:

[('12345', '54321'), ('98765', '56789')]

Yes you can use list comprehension here. Following code will give you your desired results

[tuple(sub_dict.get('ext_id') for sub_dict in dictionary.get('subdata')) for dictionary in data]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM