Reputation: 75
Im new to python and I stumbled on this problem, I would like to call a specific element in an array, but the array name is controlled by another array:
array1 = ["foo","bar","fubar"]
array2 = ["array1","array3","array4"]
number = 2
inter = array2[0]
test = inter[number]
#what im trying to achieve: test = array1[2]
#expecting: fubar
#what im getting: r
print(test)
Thanks guys :)
Upvotes: 1
Views: 2702
Reputation: 11907
What you want to do is use the array that you need not the name of the array.
To make things work you have to do like shown below. Assuming array3 and array4 are defined earlier
array1 = ["foo","bar","fubar"]
array3 = []
array4 = []
array2 = [array1,array3,array4]
number = 2
inter = array2[0]
test = inter[number]
print(test)
Upvotes: 0
Reputation: 5589
Don't use strings but references to the arrays.
array1 = ["foo","bar","fubar"]
array2 = [array1, array3, array4] # use references instead of strings here
number = 2
inter = array2[0]
test = inter[number]
print(test)
Upvotes: 3