Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Sorting a list of integers brings totally different set of integers in Python

I am new to Python and I am trying to sort a list of integers.

The input is from a text file containing lines of integers.
I convert the lines to a list of integers and then I trying to sort the list with .sort(), smallest integer first and biggest last.

I have two debugging prints to check the data. It seems, that the list of integers is OK before the .sort() but after sort the list contains nonsense: repeating integers not seemingly related to the list that was being sorted.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

my code:

    file_name = input("Give the name of file to process: ")
    file = open(file_name, 'r', encoding="utf-8")
    
    line_list = file.readlines()
    numbers = []
    for line in line_list:
        line = line.strip()
        numbers.append(int(line))
    print(numbers)
    numbers.sort()
    print(numbers)

and here is the beginning of the print(numbers) before .sort():

[53, 86, 42, 66, 79, 34, 77, 39, 89, 78, 52, 80, 36, 80, 44, 54, 43, 39, 32, 57, 88, 41, 76, 83, 55, 33, 44, 60, 63, 90, 39, 44, 71, 68, 86, 74, 82, 39, 80, 35, 36, 57, 68, 70, 32, 68, 50, 50, 55, 71, 83, 62, 42, 68, 39, 47, 80, 66, 53, 55, 56, 53, 
91, 83, 34, 54, 66, 76, 84, 30, 69, 43, 31, 90, 83, 68, 69, 76, 39, 59, 43, 69, 47, 75, 67, 68, 33, 42, 74, 87, 79, 54, 72, 31, 60, 64, 38, 40, 48, 50, 46, 78, 84, 30, 39, 62, 85, 73, 72, 75, 63, 40, 45, 72, 43, 62, 86, 36, 37, 67, 35, 77, 46, 68, 80, 91, 75, 34, 61, 75, 56, 91, 82, 53, 37, 59, 69, 68, 44, 40, 50, 39, 35, 36, 57, ...]

and after .sort()

[30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 
30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 
30, 30, 30, 30, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, ...]

I omitted rests of the prints, as the file is large. However, I am sure that the second list is wrong, because the largest number after sort is 45.

I tried it also with numbers = sorted(numbers) and it produces somewhat similar results.

Also, I tried replacing the numbers with a short list of integers, like [23,13,4,65,2] and then the sort functioned fine, as documented in several questions and documentation.

I think the bug must be with my data somewhere. Maybe reading from the file brings some artefact that messes up .sort() or what could it be?

I know the whole data would be relevant to the question, but it is too big to fit in to this question and so are the prints too.

Could someone have answers with this information?

>Solution :

Input:

53
86 
42 
66 
79 
34 
77 
39 
89 

Use sorted(numbers).

numbers = []

with open('input.txt', 'r') as f:
    lines = f.readlines()
    for line in lines:
        line = line.strip()
        numbers.append(int(line))

print(numbers)   
print(sorted(numbers)) 

Ouput:

[53, 86, 42, 66, 79, 34, 77, 39, 89, 78]
[34, 39, 42, 53, 66, 77, 78, 79, 86, 89]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading