Some remarks on sorting

You can either use the sorted() Python built-in function which will produce a sorted version of anything you can iterate over. Lists have a .sort() method which sorts them in-place.


In [1]:
l1=[3,1,4,6,7]
l2=sorted(l1)
print(l1,l2) #l2 is a different list from l1
l1.sort() #now l1 is sorted in-place
print(l1)


[3, 1, 4, 6, 7] [1, 3, 4, 6, 7]
[1, 3, 4, 6, 7]

Sort in reverse order:

reverse=True


In [2]:
print(sorted(l1,reverse=True))


[7, 6, 4, 3, 1]

Define the sort order

You can define a function which extracts the key by which to sort from the elements of the array you are sorting. For example, if you want to sort strings by their length, you would write a function which gives the length of the string and tell sort() to use it. Like so:


In [3]:
def lenk(s):
    """This function simply returns the length of s"""
    return len(s)

l=["sjsjsj","zzz","aaaaaaaaaaaaaaaaaa"]
print("Default sort order:", sorted(l))
print("New sort order by length:", sorted(l,key=lenk)) #Note: not lenk(), just lenk
print("New sort order by length (lambda):", sorted(l,key=lambda s: len(s))) #Same but with lambda functions


Default sort order: ['aaaaaaaaaaaaaaaaaa', 'sjsjsj', 'zzz']
New sort order by length: ['zzz', 'sjsjsj', 'aaaaaaaaaaaaaaaaaa']
New sort order by length (lambda): ['zzz', 'sjsjsj', 'aaaaaaaaaaaaaaaaaa']

This is very useful for example when sorting dictionaries by their value:


In [4]:
s="ACGTTGGCCAGATCGACTATGGCAGATTGACTAGCATACGATCGCATCAGAT"
counter={}
for char in s:
    counter[char]=counter.get(char,0)+1 #Easy way to count items into a dictionary
#Now we want to print the dictionary sorted by count from most to least common and 
#in case two letters have the same count, we want them sorted alphabetically

def get_val(key_value):
    """Gets one key-value pair from the dictionary and returns the value, and then the key"""
    return key_value[1], key_value[0] #the value is the first thing we sort on, the letter the second

items=list(counter.items()) #all items from the dictionary as a list of (key,value) pairs
print("Items:", items)
items.sort(reverse=True, key=get_val)
for letter,count in items:
    print(letter, count)


Items: [('A', 15), ('C', 12), ('G', 13), ('T', 12)]
A 15
G 13
T 12
C 12

Whoops. That doesn't quite do what we wanted since C should come before T. Think why that is. We want the dictionary sorted from most common to least common and in case of same-count, alphabetically. Now it's reverse-alphabetically. I'll make this into an exercise. Hint: Note the ord() function.


In [ ]: