I am trying to improve a code that I’ve made. The idea is to automate some data processing.
The idea is to link two dictionaries so that I can “easily” move from key/values and format outputs.
I now have :
my_first_dict = {"value_1": "value_1_formatted", value_2: "value_2_formatted", ...}
my_second_dict = {"value_1_formatted": "value_1_formatted_differently", "value_2_formatted": "value_2_formatted_differently", ...")
Doing so allows me to identify and rename as I want using key/value pairs. However, it can sometimes make code heavier and less readable.
I’d like to know if there is a more efficient way to proceed. I was wondering if, for example, we could have a “three layer dictionary”, with values/keys accessible via : .keys()
or .values()[...]
.
That would look like :
my_dict = {"value_1": "value_1_formatted": "value_1_formatted_differently"}
Where my_dict["value_1"].values()[1]
would return "value_1_formatted_differently"
(it doesn’t work, it’s just to present my idea of it)
What do you think about ?
5
That “three layer dictionary” syntax is obviously not valid, but you can achieve the same idea by just doing a nested lookup
>>> my_second_dict[my_first_dict["value_1"]]
'value_1_formatted_differently'
In other words, starting with your key first look up the value from the first dict, then use that as the key for the next lookup.
2
If "value_1"
is your common name, true key and "value_1_formatted"
and "value_1_formatted_differently"
are aliases that you would like to uncover based on the key/common name, then I would likely use something like this nested dictionary:
my_lookup = {
"Jonny": {
"formatted": "Jon",
"formatted_differently": "Jonathan"
},
"Annie": {
"formatted": "Anna",
"formatted_differently": "Anastasia"
},
}
Then you can:
key = "Annie"
print(my_lookup[key]["formatted_differently"])
This also might allow you to have incomplete data. For example:
my_lookup = {
"Jonny": {
"formatted": "Jon",
"formatted_differently": "Jonathan"
},
"Annie": {
"formatted": "Anna",
##"formatted_differently": "Anastasia" <--- Now missing
},
}
Then
key = "Annie"
print(my_lookup[key].get("formatted_differently", key))
1
I would suggest using 2D array, e.g. list of lists to store 2 different formats for each key, for example
columns = {"column1":0,"column2":1,"column3":2} # associate names with 0-based column names
rows = {"firstformat":0,"secondformat":1} # associate names with 0-based row names
array = [["value1format1","value2format1","value3format1"],["value1format2","value2format2","value3format2"]] # each sublist is one row
print(array[rows["secondformat"]][columns["column3"]]) # output is value3format2
If you are allowed to use external packages you might pandas.DataFrame for that purpose following way
import pandas as pd
df = pd.DataFrame([["value1format1","value2format1","value3format1"],["value1format2","value2format2","value3format2"]],columns=("column1","column2","column3"),index=("firstformat","secondformat"))
print(df["column3"]["secondformat"])