I’d like how to create and print associative arrays in python3… like in bash I do:
JavaScript
x
6
1
declare -A array
2
array["alfa",1]="text1"
3
array["beta",1]="text2"
4
array["alfa",2]="text3"
5
array["beta",2]="text4"
6
In bash I can do echo "${array["beta",1]}"
to access data to print “text2”.
How can I define a similar array in python3 and how to access to data in a similar way? I tried some approaches, but none worked.
Stuff like this:
JavaScript
1
6
1
array = ()
2
array[1].append({
3
'alfa': "text1",
4
'beta': "text2",
5
})
6
But I can’t access to data with print(array['beta', 1])
. It is not printing “text2” :(
Advertisement
Answer
It looks like you want a dictionary with compound keys:
JavaScript
1
9
1
adict = {
2
("alfa", 1): "text1",
3
("beta", 1): "text2",
4
("alfa", 2): "text3",
5
("beta", 2): "text4"
6
}
7
8
print(adict[("beta", 1)])
9