Skip to content
Advertisement

How do i sort some of the numbers from a text file and order it in higher to lower. Python

So, I have file which looks like this

JavaScript

My objective is to order it like this

JavaScript

So it would order the number of visits from higher to lower.

I’ve found a solution, which looks like this.

JavaScript

Though, this would only work if there’s a integer on the last character.

So it won’t work with the files I need it too.

My problem is on getting the numerical values from the number of visits and ordering them into ascending order, without removing anything from the file.

Sorry if this is wordy, I dont speak english.

Advertisement

Answer

This solution uses the re module.

The regex pattern I used of r"Number of visits: (d*) -" is actually larger than it needs to be and could be reduced to r": (d*) -", but I wanted it to be clear and explicit which digits it should be capturing.
If you aren’t familiar with re/Regular Expressions, the parentheses indicate that whatever matches the pattern inside of them should be captured separately from the matching string. d* means to capture any number of consecutive digits.

Each line and the extracted value are then put into a tuple and stored in the list data. I chose to convert the value to an int() at this time but it could also be done as part of the sort lambda function instead.

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement