Reputation: 871
Short question, how do I make this conversion using Python?
a["1-3-6-3-6"] ---> a[1][3][6][3][6]
I have a nested list and I want to be able to get the item, directly from a string argument passed to the method.
Upvotes: 1
Views: 557
Reputation: 906
This might be what you want
>>> i="1-3-6-3-6"
>>> b=i.split("-")
>>> b
['1', '3', '6', '3', '6']
Then you can use the indices inside b to descend into your nested list by recursion.
Upvotes: 0
Reputation: 387557
>>> path = '1-3-6-3-6'
>>> element = a
>>> for segment in path.split('-'):
element = element[int(segment)]
After that, element
equals to whatever was at a[1][3][6][3][6]
.
Upvotes: 3