data = {"c":{"files":"s","d":{"files":["nothing"]}}}
positions = ["c","d","files"]
How can I get to position I have in list "positions"? I need to get from list "positions" something like this:
data["c"]["d"]["files"]
I also need to work with list which is on that position. I already tried something, but I couldn't work with that list in way I would want to.
def goto(data,positions):
temp = data
for i in positions:
temp = temp[i]
Is there any cool way to do it?
This is a job for reduce
!
reduce(lambda a,b: a[b], positions, data)
If you're using Python 3, you'll need to import reduce
like so:
from functools import reduce
In Python 2, it's a built-in function, and thus doesn't need importing.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.