Created
January 31, 2019 17:45
-
-
Save Guilouf/5631107354af6dc1af449572d388c0f3 to your computer and use it in GitHub Desktop.
Access list dict pass via a tuple of indexes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def search_index(obj, itero): | |
try: | |
return search_index(obj[next(itero)], itero) | |
except StopIteration: | |
return obj | |
a = [{"name":"someone", "age":23}] | |
s = (0, 'name') | |
result = search_index(a, iter(s)) | |
# >>> result = 'someone' | |
#If you want to convert "[0]['name']" to (0, 'name') you can try this: | |
import re | |
reg = re.findall(r'\[(\d*?)\]|\[\'(\w*?)\'\]', "[0]['name']") | |
s = [int(index[0]) if not index[1] else index[1] for index in reg] | |
# [0, 'name'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment