0

I have a Python list created with fuzzywuzzy

result=[(fuzz.WRatio(n, n2),n2,sdf.index[x],bdf.index[y])
                    for y, n2 in enumerate(Col2['name']) if fuzz.WRatio(n, n2)>70 and len(n2) >= 2]
    print('Result: {}'.format(result))

which will produce the following result:

String to compare to raw data: ABC
Result: [(90, u'ABC COMPANY', 21636, 100079), (86, u'ABC COMPANY CO', 21636, 72933), (86, u'ABC (M) SDN BHD', 21636, 92592), (95, u'ABC PTE LTD.', 21636, 171968)]

I would like to be able to sort the whole list by the first digit of each array in descending order. I have tried using

result.sort()

which doesn't sort the list in descending order, any idea?

1
  • 1
    Using a lambda function as key for sort should work. Commented Aug 24, 2018 at 2:27

2 Answers 2

2
from operator import itemgetter

values = [(90, u'ABC COMPANY', 21636, 100079), (86, u'ABC COMPANY CO', 21636, 72933), (86, u'ABC (M) SDN BHD', 21636, 92592), (95, u'ABC PTE LTD.', 21636, 171968)]
values.sort(key=itemgetter(0), reverse=True)
print(values)

Output

[(95, 'ABC PTE LTD.', 21636, 171968), (90, 'ABC COMPANY', 21636, 100079), (86, 'ABC COMPANY CO', 21636, 72933), (86, 'ABC (M) SDN BHD', 21636, 92592)]

One alternative is to use a lambda function as key:

values.sort(key=lambda x: x[0], reverse=True)
Sign up to request clarification or add additional context in comments.

Comments

1

list.sort() has a reverse kwarg. Use it

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.