python - Fetching values preceding a given index value in json field -




i have jsonfield

[{"0":"z","1":"y","2":"x","3":"w","4":"v"}]. 

i want fetch values preceding y i.e. x,w,v ...

i = 0 name = none  obj= model.objects.get(name=request['name']) key in obj:     currentposition = key[str(i)]     = +1     if currentposition == request['position']:         continue     else:         senddata.append({"position": currentposition}) 

when adding y in request fetching details z also.

you can values preceding "y" using simple comparison in list comprehension:

>>> lst = [{"0":"z","1":"y","2":"x","3":"w","4":"v"}] >>> [v v in lst[0].values() if v < "y"] ['x', 'w', 'v'] 

to values keys preceding key of "y", use this:

>>> idx = next(int(k) k, v in lst[0].items() if v == "y") >>> [v k, v in lst[0].items() if int(k) < idx] ['z'] 




wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -