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

75%
+4 −0
Q&A Slicing a dictionary using a string variable

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 at...

posted 1y ago by mr Tsjolder‭

Answer
#1: Initial revision by user avatar mr Tsjolder‭ · 2023-07-26T14:27:44Z (over 1 year ago)
Assuming that `dynamicmodel` and `uncertainlibrary` are just (nested) [dictionaries](https://docs.python.org/3/tutorial/datastructures.html?highlight=dictionary#dictionaries) and 
```python
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:
```text
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:
```python
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](https://docs.python.org/3/tutorial/datastructures.html?highlight=dictionary#tuples-and-sequences).

Note that I took the liberty to take your variables and introduced a new variable to adhere to the [PEP-8](https://peps.python.org/pep-0008/) formatting guidelines (especially variable naming and line length).