Python - Sorting Lists, Tuples, and Objects
** Sorting Lists, Tuples, and Objects
==========================================
li = [9, 1, 8, 2, 7, 3, 6, 4, 5]
s_li = sorted(li)
print('Sorted Variable:\t', s_li)
print('Original Variable:\t', li)
---------------------------------
Sorted Variable: [1, 2, 3, 4, 5, 6, 7, 8, 9]
Original Variable: [9, 1, 8, 2, 7, 3, 6, 4, 5]
---------------------------------
==========================================
li = [9, 1, 8, 2, 7, 3, 6, 4, 5]
s_li = sorted(li)
print('Sorted Variable:\t', s_li)
li.sort()
print('Original Variable:\t', li)
---------------------------------
Sorted Variable: [1, 2, 3, 4, 5, 6, 7, 8, 9]
Original Variable: [1, 2, 3, 4, 5, 6, 7, 8, 9]
---------------------------------
==========================================
li = [9, 1, 8, 2, 7, 3, 6, 4, 5]
s_li = li.sort()
print('Sorted Variable:\t', s_li)
li.sort()
print('Original Variable:\t', li)
---------------------------------
Sorted Variable: None
Original Variable: [1, 2, 3, 4, 5, 6, 7, 8, 9]
---------------------------------
==========================================
li = [9, 1, 8, 2, 7, 3, 6, 4, 5]
s_li = sorted(li, reverse = True)
print('Sorted Variable:\t', s_li)
li.sort()
print('Original Variable:\t', li)
---------------------------------
Sorted Variable: [9, 8, 7, 6, 5, 4, 3, 2, 1]
Original Variable: [1, 2, 3, 4, 5, 6, 7, 8, 9]
---------------------------------
==========================================
li = [9, 1, 8, 2, 7, 3, 6, 4, 5]
s_li = sorted(li, reverse = True)
print('Sorted Variable:\t', s_li)
li.sort(reverse = True)
print('Original Variable:\t', li)
---------------------------------
Sorted Variable: [9, 8, 7, 6, 5, 4, 3, 2, 1]
Original Variable: [9, 8, 7, 6, 5, 4, 3, 2, 1]
---------------------------------
==========================================
tup = (9, 1, 8, 2, 7, 3, 6, 4, 5)
tup.sort()
---------------------------------
tup.sort()
AttributeError: 'tuple' object has no attribute 'sort'
---------------------------------
==========================================
tup = (9, 1, 8, 2, 7, 3, 6, 4, 5)
s_tup = sorted(tup)
print('Tuple:\t', s_tup)
---------------------------------
Tuple: [1, 2, 3, 4, 5, 6, 7, 8, 9]
---------------------------------
==========================================
di = {'name': 'Corey', 'job': 'programming', 'age': None, 'os': 'Mac'}
s_di = sorted(di)
print('Dict:\t', s_di)
---------------------------------
Dict: ['age', 'job', 'name', 'os']
---------------------------------
==========================================
li = [-6, -5, -4, 1, 2, 3]
s_li = sorted(li)
print(s_li)
---------------------------------
[-6, -5, -4, 1, 2, 3]
---------------------------------
==========================================
li = [-6, -5, -4, 1, 2, 3]
s_li = sorted(li, key = abs)
print(s_li)
---------------------------------
[1, 2, 3, -4, -5, -6]
---------------------------------
==========================================
class Employee():
def __init__(self, name, age, salary):
self.name = name
self.age = age
self.salary = salary
def __repr__(self):
return '({}, {}, ${})'.format(self.name, self.age, self.salary)
e1 = Employee('Carl', 37, 70000)
e2 = Employee('Sarah', 29, 80000)
e3 = Employee('John', 43, 90000)
employees = [e1, e2, e3]
s_employees = sorted(employees)
print(s_employees)
---------------------------------
s_employees = sorted(employees)
TypeError: '<' not supported between instances of 'Employee' and 'Employee'
---------------------------------
==========================================
class Employee():
def __init__(self, name, age, salary):
self.name = name
self.age = age
self.salary = salary
def __repr__(self):
return '({}, {}, ${})'.format(self.name, self.age, self.salary)
e1 = Employee('Carl', 37, 70000)
e2 = Employee('Sarah', 29, 80000)
e3 = Employee('John', 43, 90000)
employees = [e1, e2, e3]
def e_sort(emp):
return emp.name
s_employees = sorted(employees, key=e_sort)
print(s_employees)
---------------------------------
[(Carl, 37, $70000), (John, 43, $90000), (Sarah, 29, $80000)]
---------------------------------
==========================================
class Employee():
def __init__(self, name, age, salary):
self.name = name
self.age = age
self.salary = salary
def __repr__(self):
return '({}, {}, ${})'.format(self.name, self.age, self.salary)
e1 = Employee('Carl', 37, 70000)
e2 = Employee('Sarah', 29, 80000)
e3 = Employee('John', 43, 90000)
employees = [e1, e2, e3]
def e_sort(emp):
return emp.age
s_employees = sorted(employees, key=e_sort)
print(s_employees)
---------------------------------
[(Sarah, 29, $80000), (Carl, 37, $70000), (John, 43, $90000)]
---------------------------------
==========================================
class Employee():
def __init__(self, name, age, salary):
self.name = name
self.age = age
self.salary = salary
def __repr__(self):
return '({}, {}, ${})'.format(self.name, self.age, self.salary)
e1 = Employee('Carl', 37, 70000)
e2 = Employee('Sarah', 29, 80000)
e3 = Employee('John', 43, 90000)
employees = [e1, e2, e3]
def e_sort(emp):
return emp.salary
s_employees = sorted(employees, key=e_sort)
print(s_employees)
---------------------------------
[(Carl, 37, $70000), (Sarah, 29, $80000), (John, 43, $90000)]
---------------------------------
==========================================
class Employee():
def __init__(self, name, age, salary):
self.name = name
self.age = age
self.salary = salary
def __repr__(self):
return '({}, {}, ${})'.format(self.name, self.age, self.salary)
e1 = Employee('Carl', 37, 70000)
e2 = Employee('Sarah', 29, 80000)
e3 = Employee('John', 43, 90000)
employees = [e1, e2, e3]
def e_sort(emp):
return emp.salary
s_employees = sorted(employees, key=e_sort, reverse = True)
print(s_employees)
---------------------------------
[(John, 43, $90000), (Sarah, 29, $80000), (Carl, 37, $70000)]
---------------------------------
==========================================
class Employee():
def __init__(self, name, age, salary):
self.name = name
self.age = age
self.salary = salary
def __repr__(self):
return '({}, {}, ${})'.format(self.name, self.age, self.salary)
e1 = Employee('Carl', 37, 70000)
e2 = Employee('Sarah', 29, 80000)
e3 = Employee('John', 43, 90000)
employees = [e1, e2, e3]
def e_sort(emp):
return emp.salary
s_employees = sorted(employees, key=lambda e: e.name)
print(s_employees)
---------------------------------
[(Carl, 37, $70000), (John, 43, $90000), (Sarah, 29, $80000)]
---------------------------------
==========================================
class Employee():
def __init__(self, name, age, salary):
self.name = name
self.age = age
self.salary = salary
def __repr__(self):
return '({}, {}, ${})'.format(self.name, self.age, self.salary)
from operator import attrgetter
e1 = Employee('Carl', 37, 70000)
e2 = Employee('Sarah', 29, 80000)
e3 = Employee('John', 43, 90000)
employees = [e1, e2, e3]
# def e_sort(emp):
# return emp.salary
s_employees = sorted(employees, key = attrgetter('age'))
print(s_employees)
---------------------------------
[(Sarah, 29, $80000), (Carl, 37, $70000), (John, 43, $90000)]
---------------------------------