Richard Rublev
Richard Rublev

Reputation: 8164

Python sort in unproper manner

My code

class getCol:
    matrix = []
    def __init__(self, file, delim=" "):
        with open(file, 'rU') as f:
            getCol.matrix =  [filter(None, l.split(delim)) for l in f]

    def __getitem__ (self, key):
        column = []
        for row in getCol.matrix:
            try:
                column.append(row[key])
            except IndexError:
                # pass
                column.append("")
        return column

list1 = getCol('/home/milenko/EDIs/site1/newst2.txt')[0]
list2 = getCol('/home/milenko/EDIs/site2/newst2.txt')[0]
list3 = getCol('/home/milenko/EDIs/site3/newst2.txt')[0]
list4 = getCol('/home/milenko/EDIs/site4/newst2.txt')[0]
list5 = getCol('/home/milenko/EDIs/site5/newst2.txt')[0]
list6 = getCol('/home/milenko/EDIs/site6/newst2.txt')[0]
list7 = getCol('/home/milenko/EDIs/site7/newst2.txt')[0]
list8 = getCol('/home/milenko/EDIs/site8/newst2.txt')[0]
list9 = getCol('/home/milenko/EDIs/site9/newst2.txt')[0]
list10 = getCol('/home/milenko/EDIs/site10/newst2.txt')[0]
list11 = getCol('/home/milenko/EDIs/site11/newst2.txt')[0]
list12 = getCol('/home/milenko/EDIs/site12/newst2.txt')[0]
list13 = getCol('/home/milenko/EDIs/site13/newst2.txt')[0]
list14 = getCol('/home/milenko/EDIs/site14/newst2.txt')[0]
list15 = getCol('/home/milenko/EDIs/site15/newst2.txt')[0]

list_of_lists = []
list_of_lists.append(list1)
list_of_lists.append(list2)
list_of_lists.append(list3)
list_of_lists.append(list4)
list_of_lists.append(list5)
list_of_lists.append(list6)
list_of_lists.append(list7)
list_of_lists.append(list8)
list_of_lists.append(list9)
list_of_lists.append(list10)
list_of_lists.append(list11)
list_of_lists.append(list12)
list_of_lists.append(list13)
list_of_lists.append(list14)
list_of_lists.append(list15)

result = []

# Loop the inner lists from list_of_lists, this will be list1, list2, list3...
for inner_list in list_of_lists:
    # Loop each element of the inner lists
    for element in inner_list:
        # Make sure the element is not already in the result (this can also be done with sets)
        if element not in result:
            # Add the inner element to result
            result.append(element)

# Sort the result
result = sorted(result)
print("\n".join(map(str, result)))

But problem is here

1.92413
10.15704
1026.00000
10260.00000
10672.43359
11.81549
1104.06055
114.21478
12.00000
12415.04102
1284.33289
13.74474
132.00000
132.86391
1376.00000
13760.00000
14442.18457
1494.04028
15.00000

I just want normal ordering from smallest to largest.How should I solve this?Is there any other alternative to sort?

Upvotes: 0

Views: 33

Answers (2)

Tom Zych
Tom Zych

Reputation: 13576

It's sorting as strings. To sort as numbers, use the key argument:

result = sorted(result, key=float)

This converts each string to a float for sorting purposes, but leaves the data as is.

Since you're assigning the result to the same identifier, you can also:

result.sort(key=float)

Upvotes: 3

ben_frankly
ben_frankly

Reputation: 9940

You need to convert the values in your list from strings to a numeric type like floats.

Upvotes: 1

Related Questions