1

Help please :

myliste = ['Bullet train\t5018\n', 'Prey\t508\n', 'Treize vies\t1978\n', 'L’année du requin\t4398\n', 'Carter\t5004\n', 'Night Raiders\t1312\n', 'En décalage\t1789\n', 'The Bikeriders\t2134\n', 'Darling\t2008\n', 'Alina of Cuba\t678\n', 'Amants Super-héroïques\t837\n', 'Ménestrel\t3065\n', 'Des feux dans la nuit\t2144\n', 'Luck\t3148\n', 'Sita Ramam\t4498\n', 'Poikkal Kudhirai\t2248\n', 'Les Promesses d’Hasan\t3598\n', 'Wedding Season\t2490\n', 'Le Destin des Tortues Ninja, le Film\t845\n', 'La Vie en plus grand\t489\n', 'Hero Mode\t245\n', 'Embuscade\t1038\n', 'The Last Son\t1245\n', 'Mission Eagle\t998\n', 'Doblemente Embarazada\t745']

I want to sort this list according to the number, from the highest number to the lowest without changing the elements of my list, I have tried this :

def sort_films(LF):
        a = i.index('\t')
        b = i.index('\n')
        c = i[a+2:b]
        return(c)

LF.sort(key=sort_films,reverse=True)

the result I have is :

['Wedding Season\t2490\n', 'Treize vies\t1978\n', 'The Last Son\t1245\n', 'The Bikeriders\t2134\n', 'Sita Ramam\t4498\n', 'Prey\t508\n', 'Poikkal Kudhirai\t2248\n', 'Night Raiders\t1312\n', 'Ménestrel\t3065\n', 'Mission Eagle\t998\n', 'L’année du requin\t4398\n', 'Luck\t3148\n', 'Les Promesses d’Hasan\t3598\n', 'Le Destin des Tortues Ninja, le Film\t845\n', 'La Vie en plus grand\t489\n', 'Hero Mode\t245\n', 'En décalage\t1789\n', 'Embuscade\t1038\n', 'Doblemente Embarazada\t745', 'Des feux dans la nuit\t2144\n', 'Darling\t2008\n', 'Carter\t5004\n', 'Bullet train\t5018\n', 'Amants Super-héroïques\t837\n', 'Alina of Cuba\t678\n']

I don't know what to do please if you have a solution

3
  • It seems that I ordered well for the first values but then I have later on the list bigger values that had been forgotten and not ordered well. Commented Oct 15, 2022 at 8:38
  • add print(sort_films(myliste[0])) to your program and see what it prints Commented Oct 15, 2022 at 8:40
  • Last item 'Doblemente Embarazada\t745' has no "\n". Commented Oct 15, 2022 at 8:44

4 Answers 4

2

You can use regex and a lambda function to provide the key for sort. \d+ will find strings of at least one number in each list item. To avoid the case of a number in the title, the sort is done on the last number found in the list item.

import re
sorted_myliste =  sorted(myliste, key=lambda x: int(re.findall(r'\d+', x)[-1]), reverse=True)
Sign up to request clarification or add additional context in comments.

Comments

0

This seems to work as expected:

LF = ['Bullet train\t5018\n', 'Prey\t508\n', 'Treize vies\t1978\n', 'L’année du requin\t4398\n', 'Carter\t5004\n', 'Night Raiders\t1312\n', 'En décalage\t1789\n', 'The Bikeriders\t2134\n', 'Darling\t2008\n', 'Alina of Cuba\t678\n', 'Amants Super-héroïques\t837\n', 'Ménestrel\t3065\n', 'Des feux dans la nuit\t2144\n', 'Luck\t3148\n', 'Sita Ramam\t4498\n', 'Poikkal Kudhirai\t2248\n', 'Les Promesses d’Hasan\t3598\n', 'Wedding Season\t2490\n', 'Le Destin des Tortues Ninja, le Film\t845\n', 'La Vie en plus grand\t489\n', 'Hero Mode\t245\n', 'Embuscade\t1038\n', 'The Last Son\t1245\n', 'Mission Eagle\t998\n', 'Doblemente Embarazada\t745\n']

def sort_films(i):
        a = i.index('\t')
        try:
            b = i.index('\n')
        except:
            b = len(i)
        c = int(i[a+1:b]) # +1 instead of +2 as first digit must not be skipped
        return(c)

LF.sort(key=sort_films,reverse=True)
print(LF)

Output:

['Bullet train\t5018\n', 'Carter\t5004\n', 'Sita Ramam\t4498\n', 'L’année du requin\t4398\n', 'Les Promesses d’Hasan\t3598\n', 'Luck\t3148\n', 'Ménestrel\t3065\n', 'Wedding Season\t2490\n', 'Poikkal Kudhirai\t2248\n', 'Des feux dans la nuit\t2144\n', 'The Bikeriders\t2134\n', 'Darling\t2008\n', 'Treize vies\t1978\n', 'En décalage\t1789\n', 'Night Raiders\t1312\n', 'The Last Son\t1245\n', 'Embuscade\t1038\n', 'Mission Eagle\t998\n', 'Le Destin des Tortues Ninja, le Film\t845\n', 'Amants Super-héroïques\t837\n', 'Doblemente Embarazada\t745', 'Alina of Cuba\t678\n', 'Prey\t508\n', 'La Vie en plus grand\t489\n', 'Hero Mode\t245\n']

1 Comment

Yes it works but we have the problem of the last one which doesn't have a "\n" at the end so it has not been ordered and placed at the end where it should not be as it is a higher value than the previous ones, how can i solve that ? thank you a lot btw
0

Approach with filter + str.isdigit,

sorted(myliste, key=lambda x: int(''.join(filter(str.isdigit, x))), reverse=True)

Output:

['Bullet train\t5018\n', 'Carter\t5004\n', 'Sita Ramam\t4498\n', 'L’année du requin\t4398\n', 'Les Promesses d’Hasan\t3598\n', 'Luck\t3148\n', 'Ménestrel\t3065\n', 'Wedding Season\t2490\n', 'Poikkal Kudhirai\t2248\n', 'Des feux dans la nuit\t2144\n', 'The Bikeriders\t2134\n', 'Darling\t2008\n', 'Treize vies\t1978\n', 'En décalage\t1789\n', 'Night Raiders\t1312\n', 'The Last Son\t1245\n', 'Embuscade\t1038\n', 'Mission Eagle\t998\n', 'Le Destin des Tortues Ninja, le Film\t845\n', 'Amants Super-héroïques\t837\n', 'Doblemente Embarazada\t745', 'Alina of Cuba\t678\n', 'Prey\t508\n', 'La Vie en plus grand\t489\n', 'Hero Mode\t245\n']

Comments

0

If you are okay with using numpy and re, you can convert to numpy array and then use re to extract the numbers. Then you can sort the array with argsort.

import re
import numpy as np


def sort_films(LF, reverse=False):
    numbers = np.array([re.findall(r'\d+', s) for s in myliste]).flatten().astype(int)
    if reverse==False:
        return list(np.array(myliste)[np.argsort(numbers)])
    else:
        return list(np.array(myliste)[np.argsort(numbers)][::-1])

Then sort_films(myliste) outputs:

['Hero Mode\t245\n', 'La Vie en plus grand\t489\n', 'Prey\t508\n',
   'Alina of Cuba\t678\n', 'Doblemente Embarazada\t745',
   'Amants Super-héroïques\t837\n',
   'Le Destin des Tortues Ninja, le Film\t845\n',
   'Mission Eagle\t998\n', 'Embuscade\t1038\n',
   'The Last Son\t1245\n', 'Night Raiders\t1312\n',
   'En décalage\t1789\n', 'Treize vies\t1978\n', 'Darling\t2008\n',
   'The Bikeriders\t2134\n', 'Des feux dans la nuit\t2144\n',
   'Poikkal Kudhirai\t2248\n', 'Wedding Season\t2490\n',
   'Ménestrel\t3065\n', 'Luck\t3148\n',
   'Les Promesses d’Hasan\t3598\n', 'L’année du requin\t4398\n',
   'Sita Ramam\t4498\n', 'Carter\t5004\n', 'Bullet train\t5018\n']

and sort_films(myliste, reverse=True) gives

['Bullet train\t5018\n', 'Carter\t5004\n', 'Sita Ramam\t4498\n',
   'L’année du requin\t4398\n', 'Les Promesses d’Hasan\t3598\n',
   'Luck\t3148\n', 'Ménestrel\t3065\n', 'Wedding Season\t2490\n',
   'Poikkal Kudhirai\t2248\n', 'Des feux dans la nuit\t2144\n',
   'The Bikeriders\t2134\n', 'Darling\t2008\n', 'Treize vies\t1978\n',
   'En décalage\t1789\n', 'Night Raiders\t1312\n',
   'The Last Son\t1245\n', 'Embuscade\t1038\n',
   'Mission Eagle\t998\n',
   'Le Destin des Tortues Ninja, le Film\t845\n',
   'Amants Super-héroïques\t837\n', 'Doblemente Embarazada\t745',
   'Alina of Cuba\t678\n', 'Prey\t508\n',
   'La Vie en plus grand\t489\n', 'Hero Mode\t245\n']

Comments

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.