i have this tuple:
STATUS = ((1, 'Standby'), (2, 'Approved'), (3, 'Rejected'))
The problem seems to be that this is a tuple (x,x,x)
inside other tuple where x
is (i,'string')
. I need to get a index from the child tuple this is possible by doing this STATUS[0].index('Standby')
but this is not a good solution because i can't find directly by name.
So, i want to find the index directy without to mention what is the position of the tuple that i'm seeking.
Why not use a list and then use index?
STATUS = [None, 'Standby', 'Approved', 'Rejected']
STATUS.index('Standby')
returns 1
Well,
it seems that doesn't exist any solution by default. So you need to do a function. Using the suggestion of @tobias_k i made this possible solution.
def deepindex(mytuple,myvalue):
for i in mytuple:
if i.index(myvalue):
return i.index(myvalue)
>>> deepindex(STATUS,'Standby')
>>> 1
Please if you find a better solution let me know. Thanks ;)
Zip and map seems to be a good approach
zip: aggregates elements from each of the iterables (*STATUS) (see docs )
map: to every item of iterable and return a list of the results (see docs )
Using this two functions: you can get the index of a tuple inside another tuple as is described on this question as STATUS.
>>> a,b= map(list,zip(*STATUS))
>>> a[b.index('Standby')]
>>> 1
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.