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)
In [2]:
print(sorted(l1,reverse=True))
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
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)
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 [ ]: