planets = [
('Mercury',2440,5.43,0.395),
('Venus',6052,5.24,0.723),
('Earth',6378,5.52,1.000),
('Mars',3396,3.93,1.530)
]
size = lambda planet: planet[1]
planets.sort(key=size,reverse=True)
print(planets)
# [('Earth', 6378, 5.52, 1.0), ('Venus', 6052, 5.24, 0.723), ('Mars', 3396, 3.93, 1.53), ('Mercury', 2440, 5.43, 0.395)]
size2 = lambda planet: planet[2]
planets.sort(key=size2,reverse=True)
print(planets)
# [('Earth', 6378, 5.52, 1.0), ('Mercury', 2440, 5.43, 0.395), ('Venus', 6052, 5.24, 0.723), ('Mars', 3396, 3.93, 1.53)]
size3 = lambda planet: planet[3]
planets.sort(key=size3,reverse=True)
print(planets)
# [('Mars', 3396, 3.93, 1.53), ('Earth', 6378, 5.52, 1.0), ('Venus', 6052, 5.24, 0.723), ('Mercury', 2440, 5.43, 0.395)]
# sort 排序后更改掉了原list
# sorted 不会更改原list
earth_metal = ["Beryllium","Magnesium","Calcium","Strontium","Barium","Radius"]
sorted_earth_metals = sorted(earth_metal)
print(earth_metal)
# ['Beryllium', 'Magnesium', 'Calcium', 'Strontium', 'Barium', 'Radius']
print(sorted_earth_metals)
# ['Barium', 'Beryllium', 'Calcium', 'Magnesium', 'Radius', 'Strontium']
data = (7,2,5,6,1,3,9,10,4,8)
print(sorted(data))
# sorted tuple[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(data)
# (7, 2, 5, 6, 1, 3, 9, 10, 4, 8)
print(sorted("Asopauiodsuds"))
# ['A', 'a', 'd', 'd', 'i', 'o', 'o', 'p', 's', 's', 's', 'u', 'u']