Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

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.

Post History

50%
+2 −2
Q&A 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 ...

1 answer  ·  posted 1y ago by megalomaniac‭  ·  edited 1y ago by megalomaniac‭

Question python
#2: Post edited by user avatar megalomaniac‭ · 2023-07-26T15:38:32Z (over 1 year ago)
  • 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.
  • 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.
#1: Initial revision by user avatar megalomaniac‭ · 2023-07-25T22:17:33Z (over 1 year ago)
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.