Split dictionary based on values

2024/10/12 8:19:45

I have a dictionary:

data = {'cluster': 'A', 'node': 'B', 'mount': ['C', 'D', 'E']}

I'm trying to split the dictionary data into number of dictionaries based on values in key mount.

I tried using:

for value in data.items():print(data)

But I get this:

data = {'cluster': 'A', 'node': 'B', 'mount': ['C', 'D', 'E']}
data = {'cluster': 'A', 'node': 'B', 'mount': ['C', 'D', 'E']}
data = {'cluster': 'A', 'node': 'B', 'mount': ['C', 'D', 'E']}

Actually, I would like to get:

data = {'cluster': 'A', 'node': 'B', 'mount': 'C'}
data = {'cluster': 'A', 'node': 'B', 'mount': 'D'}
data = {'cluster': 'A', 'node': 'B', 'mount': 'E'}
Answer

You could use a list comprehension with itertools.product:

>>> from itertools import product
>>> [dict(zip(data.keys(), prod)) for prod in product(*data.values())]
[{'cluster': 'A', 'mount': 'C', 'node': 'B'},{'cluster': 'A', 'mount': 'D', 'node': 'B'},{'cluster': 'A', 'mount': 'E', 'node': 'B'}]

It would even work if another variable contains a list:

>>> data = {'cluster': ['A', 'B'], 'node': 'B', 'mount': ['C', 'D', 'E']}
>>> [dict(zip(data.keys(), prod)) for prod in product(*data.values())]
[{'cluster': 'A', 'mount': 'C', 'node': 'B'},{'cluster': 'B', 'mount': 'C', 'node': 'B'},{'cluster': 'A', 'mount': 'D', 'node': 'B'},{'cluster': 'B', 'mount': 'D', 'node': 'B'},{'cluster': 'A', 'mount': 'E', 'node': 'B'},{'cluster': 'B', 'mount': 'E', 'node': 'B'}]
https://en.xdnf.cn/q/118217.html

Related Q&A

Using defaultdict to parse multi delimiter file

I need to parse a file which has contents that look like this:20 31022550 G 1396 =:0:0.00:0.00:0.00:0:0:0.00:0.00:0.00:0:0.00:0.00:0.00 A:2:60.00:33.00:37.00:2:0:0.02:0.02:40.00:2:0.98:126.00…

Iterating in DataFrame and writing down the index of the values where a condition is met

I have a data made of 20 rows and 2500 columns. Each column is a unique product and rows are time series, results of measurements. Therefore each product is measured 20 times and there are 2500 product…

Access denied to ClearDB database using Python/Django on Heroku

Im trying to build a webapp on Heroku using Python/Django, and I just followed the tutorial to set up a Django project and push it to Heroku. However, I can never even get to the normal Django "I…

Replacing a line in a file based on a keyword search, by line from another file

Here is my file1: agadfadsdffasdfElement 1, 0, 0, 0PcomElement 2Here is my file2: PBARElement 1, 100, 200, 300, 400Element 2Continue...I want to search with a keyword, "Element 1" in file1,…

How to check for pop up alert using selenium in python

What I want is to continue with the next iteration if there is a pop up message in the webpage being scrapped. That is if there is any pop up message, I want to accept that message and go to the next i…

Rally host is non-existent or unreachable via pyral

I am trying to call rally server simply using below: rally = Rally(server, user, password, workspace=workspace, project=project)But it is giving below error:Traceback (most recent call last):File "…

Query tangled array in Pymongo

I am trying to query a very tangled collection. The schema:{tags: {variables: [{value: 3x9, var_name: s},{value: 12:00AM, var_name: x},{value: goog, var_name: y}]},url: https://www.google.com}]The Quer…

manipulating value of pandas dataframe cell based on value in previous row without iteration

I have a pandas dataframe with~3900 rows and 6 columns compiled from Google Finance . One of these columns defines a time in unix format, specifically defining a time during the trading day for a marke…

Convert ctypes code to cython

Id like to convert some ctypes code to use cython instead, but Im struggling. Essentially, the ctypes code:copies the contents (floats) of two lists into C-compatible structs sends the structs to my b…

Enable PyROOT Ubuntu 14.04

I downloaded madpgraph5, but when I run it I get the following error:ERROR: ROOT file called ROOT.py or ROOT.pyc is not foundERROR: Please check that ROOT is properly installed.When I try locate ROOT.p…