3.3.1方法sort() 对列表进行永久性排序
#!/usr/bin/python
# -*- coding: latin-1 -*-
import os, sys
#字母顺序,修改是永久性的
cars = ['bmw','audi','toyota','subaru']
cars.sort()
print(cars)
output:
['audi', 'bmw', 'subaru', 'toyota']
#字母反序,修改是永久性的
cars.sort(reverse=True)
print(cars)
output:
['toyota', 'subaru', 'bmw', 'audi']
#临时排序,可按照特定顺序排列,同时不影响原始排列顺序
cars = ['bmw','audi','toyota','subaru']
print("Here is the original list:")
print(cars)
print("\nHere is the sorted list:")
print(sorted(cars))
#cars.sorted(reverse=True)
#print(cars)
print("\nHere is the original list again:")
print(cars)
output:
Here is the original list:
['bmw', 'audi', 'toyota', 'subaru']
Here is the sorted list:
['audi', 'bmw', 'subaru', 'toyota']
Here is the original list again:
['bmw', 'audi', 'toyota', 'subaru']
#倒着打印列表,反转列表元素排列顺序
cars = ['bmw','audi','toyota','subaru']
print(cars)
cars.reverse()
print(cars)
#reverse 永久性修改元素排列顺序,再次调用reverse可恢复至原始顺序
print("\nThis is the reversed list:")
print(cars)
cars.reverse()
print("\nThis is the reversed twice list:")
print(cars)
output:
['bmw', 'audi', 'toyota', 'subaru']
['subaru', 'toyota', 'audi', 'bmw']
This is the reversed list:
['subaru', 'toyota', 'audi', 'bmw']
This is the reversed twice list:
['bmw', 'audi', 'toyota', 'subaru']
#确定列表的长度,计数时从1开始
cars = ['bmw','audi','toyota','subaru']
print(len(cars))
output:
4
quiz_3-8
#!/usr/bin/python
# -*- coding:latin-1 -*-
import os, sys
city = ['Beijing','New York','London','Paris','Massachusetts']
print(city)
output:
['Beijing', 'New York', 'London', 'Paris', 'Massachusetts']
#sorted方法临时更改顺序
print("\nThis is the original list:")
print(city)
print("\nThis is the sorted list:")
print(sorted(city))
print("\nThis is the original list again: ")
print(city)
output:
This is the original list:
['Beijing', 'New York', 'London', 'Paris', 'Massachusetts']
This is the sorted list:
['Beijing', 'London', 'Massachusetts', 'New York', 'Paris']
This is the original list again:
['Beijing', 'New York', 'London', 'Paris', 'Massachusetts']
['Beijing', 'New York', 'London', 'Paris', 'Massachusetts']
#sorted()方法,字母顺序相反的顺序输出列表
print(sorted(city,reverse=True))
print(city)
output:
['Paris', 'New York', 'Massachusetts', 'London', 'Beijing']
['Beijing', 'New York', 'London', 'Paris', 'Massachusetts']
#reverse反转列表元素排列顺序
print("\nThis is the original list:")
print(city)
city.reverse()
print("\nThis is the reversed once list:")
print(city)
print("\nThis is the reversed twice list:")
city.reverse()
print(city)
output:
This is the original list:
['Beijing', 'New York', 'London', 'Paris', 'Massachusetts']
This is the reversed once list:
['Massachusetts', 'Paris', 'London', 'New York', 'Beijing']
This is the reversed twice list:
['Beijing', 'New York', 'London', 'Paris', 'Massachusetts']
#sort方法,字母顺序与字母顺序相反输出列表
print("\nThis is the original list:")
print(city)
city.sort()
print("\nThis is the sort list:")
print(city)
city.sort(reverse=True)
print("\nThis is the sort reverse true list:")
print(city)
output:
This is the original list:
['Beijing', 'New York', 'London', 'Paris', 'Massachusetts']
This is the sort list:
['Beijing', 'London', 'Massachusetts', 'New York', 'Paris']
This is the sort reverse true list:
['Paris', 'New York', 'Massachusetts', 'London', 'Beijing']
#晚餐嘉宾
guest = ['Washington','Adams','Jefferson','Madison','Bush','Obama']
message = " persons invited by me will enjoy the dinner with us students. "
num = len(guest)
print(str(num)+message)
output:
6 persons invited by me will enjoy the dinner with us students.