0

I tried to convert an unicode string like this (that I am getting from a web service):

value = [[u"Seba", u"10"], [u"[Gianfranco", u"80"], [u"[Marco", u"20"], [u"[Massimo", u"125"]] 

And I want to create a nested list in order to be able to sort it via "sorted" method.

This is what I did: First remove all the not needed ""

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

then strip extra [] and create list with split method:

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

Finally I am able to sort via sorted method, on second element of the nested list.

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

The code is working but I was wondering if there's a more elegant solution. Thanks

5

1 Answer 1

2

you could try

>>> [[i.encode('ascii', 'ignore').replace('[', '') for i in x] for x in value]
[['Seba', '10'], ['Gianfranco', '80'], ['Marco', '20'], ['Massimo', '125']]
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks but it's not correct solution because i don't get a nested list but just a list. I need to get [['Seba','10'], [['Gianfranco','80'], [['Marco','20'],[['Massimo','125']]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.