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?

dythe
  • 840
  • 5
  • 21
  • 45

2 Answers2

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)
Dani Mesejo
  • 61,499
  • 6
  • 49
  • 76
1

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

leotrubach
  • 1,509
  • 12
  • 15