Demosthanes
Demosthanes

Reputation: 329

Sorting in Python 3.3

I am trying to sort a list of 100 two-digit integers brought in from a file and for some reason the sorted() function seems to make no changes. I have already searched similar issues with the sorted function and most of them are because there is no typecast to an int which I have included. Please let me know where my logic is failing.

#!/usr/bin/python
import copy

data = []
with open('afile.txt') as file:
for line in file:
    line = line.split() # to deal with blank 
    if line:            # lines (ie skip them)
        line = [int(i) for i in line]
        data.append(copy.copy(line))
newdata = sorted(data)
print(newdata)

EDIT:

Input is simply a one to two digit number and a space between them. Ex. 17 8 97 1 26

Upvotes: 0

Views: 204

Answers (1)

John La Rooy
John La Rooy

Reputation: 304393

data ends up being a list of lists, are you sure that's what you want?

Perhaps replace

data.append(copy.copy(line))

with

data.extend(line)

Are there multiple lines each with multiple numbers on them?

Upvotes: 1

Related Questions