python - Convert Unicode string to nested list -


i tried convert unicode string (that getting web service):

value = [[u"seba", u"10"], [u"[gianfranco", u"80"], [u"[marco", u"20"], [u"[massimo", u"125"]]  

and want create nested list in order able sort via "sorted" method.

this did: first remove not needed ""

value = value.replace('"', '') 

then strip [] , create list split method:

valuelist = [x.split(',') x in value.strip('[]').split('],[')] 

finally able sort via sorted method, on second element of nested list.

valuelist = sorted(valuelist,key=lambda valuelist: int((valuelist[1])), reverse=true) 

the code working wondering if there's more elegant solution. thanks

you try

>>> [[i.encode('ascii', 'ignore').replace('[', '') in x] x in value] [['seba', '10'], ['gianfranco', '80'], ['marco', '20'], ['massimo', '125']] 

Comments