Python Forum
Need help with sorting lists as a beginner
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help with sorting lists as a beginner
#2
Please post code as text, not screen shots.

Your list is sorted alphabetically because it is a list of strings that just look like numbers. If you want to sort the list numerically, convert the strings into numbers. You can do this by changing the list items to numbers, or you can use a sorting key that uses the numerical values of the items.
original = [1, 303, 12, 40092, 32, 1002]

# Write list to a file
with open("text.txt", "w") as file:
    for item in original:
        print(item, file=file)

# Read the list from the file
with open("text.txt", "r") as file:
    from_file = file.read().strip().split("\n")

print("Original list", original, " Sorted", sorted(from_file))
print("List from file", from_file)
print("Sorted alphabetically", sorted(from_file))
print("Sorted numerically", sorted(from_file, key=int))
Output:
Original list [1, 303, 12, 40092, 32, 1002] Sorted ['1', '1002', '12', '303', '32', '40092'] List from file ['1', '303', '12', '40092', '32', '1002'] Sorted alphabetically ['1', '1002', '12', '303', '32', '40092'] Sorted numerically ['1', '12', '32', '303', '1002', '40092']
Notice that the list read from the file is full of strings, not ints. Also notice that my list read from file is shorter than yours. Your list contains a blank for the last newline. I removed the trailing newline before splitting the file input.

An alternative is to write the values using a file format that retains their type. In the example below, the list is written to a json file. When reading the file back (load()) no extra steps are required to read the file as a list of ints.
import json

original = [1, 303, 12, 40092, 32, 1002]

# Write list to a file
with open("text.txt", "w") as file:
    json.dump(original, file)

# Read the list from the file
with open("text.txt", "r") as file:
    from_file = json.load(file)

print("Original list", original)
print("List from file", from_file)
Output:
Original list [1, 303, 12, 40092, 32, 1002] List from file [1, 303, 12, 40092, 32, 1002]
Reply


Messages In This Thread
RE: Need help with sorting lists as a beginner - by deanhystad - Apr-25-2023, 04:32 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  sorting a list of lists by an element leapcfm 3 1,927 Sep-10-2021, 03:33 PM
Last Post: leapcfm
  beginner question about lists and functions sudonym3 5 2,822 Oct-17-2020, 12:31 AM
Last Post: perfringo
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,452 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  sorting list of lists pframe 5 26,195 Apr-17-2020, 09:31 PM
Last Post: Larz60+
  Beginner. Help needed in sorting sundaeli 4 2,265 Mar-21-2020, 07:16 PM
Last Post: sundaeli
  Sorting a copied list is also sorting the original list ? SN_YAZER 3 3,127 Apr-11-2019, 05:10 PM
Last Post: SN_YAZER
  sort lists of lists with multiple criteria: similar values need to be treated equal stillsen 2 3,325 Mar-20-2019, 08:01 PM
Last Post: stillsen
  Sorting lists prioritizing letters before numbers Ratherfastmofo 5 3,287 Nov-21-2018, 06:44 PM
Last Post: woooee
  Sorting list of lists with string and int Otbredbaron 6 4,255 May-07-2018, 06:04 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020