Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Comments on Slicing a dictionary using a string variable
Parent
Slicing a dictionary using a string variable
I am working on adding uncertainty to some of my dynamics within a larger Monte Carlo simulation. As a way to perform multiple operations on each value, I decided to make and use a new function to pull from multiple libraries.
Before, I was using something like the following to get the data from the dictionary:
aircraft = 'C172'
dynamicmodel = getattr(ModelLibrary, re.sub(r'[0-9]','',aircraft))
CL0 = dynamicmodel["SCD"]["CL0"]
Now, I am wanting to add a function to this call so that ["SCD"]["CL0"]
is the input to the function for example. However, I can't find a way to use this string to slice the dictionary for the correct value.
I have tried using something like the following for the function without success:
aircraft = 'C172'
uncertainty = 'Study1'
dynamicmodel = getattr(ModelLibrary, re.sub(r'[0-9]','',aircraft))
uncertainlibrary = getattr(modeluncertaintylibrary, uncertainty)
value = '["SCD"]["CL0"]'
def generate_val(dynamicmodel, uncertaintylibrary, value):
CL0 = dynamicmodel + value
uncertainty_CL0 = uncertaintylibrary + value
new_CL0 = np.random.uniform((1-uncertainty_CL0)*CL0, (1+uncertainty_CL0)*CL0)
return new_CL0
However, the data types do not agree and changing the data type does not help, so I am thinking there is probably some work around that I don't know about.
Post
Assuming that dynamicmodel
and uncertainlibrary
are just (nested) dictionaries and
value = '["SCD"]["CL0"]'
is actually supposed to be a string (note the single quotes here), your code would attempt to add a dictionary to a string, which is indeed not supported. Python would let you know by throwing an error of the form:
TypeError: unsupported operand type(s) for +: 'dict' and 'str'
If this is indeed your use case (I can't be sure due to missing error message and/or possible typos), then you could probably achieve what you want to do by using more appropriate types to represent the keys for the dictionaries:
value = ("SCD", "CL0")
def generate_val(dynamicmodel, uncertaintylibrary, value):
key1, key2 = value # unpack keys in value
model = dynamicmodel[key1][key2] # index (nested) dict
uncertainty = uncertaintylibrary[key1][key2] # index (nested) dict
deviation = uncertainty * model # additional variable
new_model = np.random.uniform(model - deviation, model + deviation)
return new_model
I chose to make the value
variable a tuple of the two keys for the nested dictionary indexing.
The parentheses are there for clarity, but they are not necessary (due to tuple packing).
The first line of the function unpacks this tuple to retrieve the two keys for indexing again.
For more information on tuple packing/unpacking, I refer to the docs.
Note that I took the liberty to take your variables and introduced a new variable to adhere to the PEP-8 formatting guidelines (especially variable naming and line length).
2 comment threads