# -*- coding: GBK -*- cars = ["bmw", "audi", "toyota", "subaru"] print("这是以前的列表:") print(cars) print("\n这是排序后的列表:") print(sorted(cars)) print("\n再次核对是否改变以前的列表:") print(cars) 输出为: 这是以前的列…
cars = ["bmw", "audi", "toyota", "subaru"] cars.sort() print(cars) 输出为: ['audi', 'bmw', 'subaru', 'toyota'] 反向排序: cars = ["bmw", "audi", "toyota", "subaru"] cars.sort(reverse…
sorted()函数对列表进行临时排序,返回排序后的列表: 区别列表方法sort()原地修改,无返回值. 1-要保留列表元素原来的排列顺序,同时以特定的顺序呈现它们,可使用函数sorted() . 2-函数sorted() 能够按特定顺序显示列表元素,同时不影响它们在列表中的原始排列顺序. 实例1:   3-如果你要按与字母顺序相反的顺序显示列表,也可向函数sorted() 传递参数reverse=True . 实例2:…
>>> cars = ["bmw", "audi", "toyota", "subaru"] >>> len(cars) 4…
# -*- coding: GBK -*- #reverse: 相反的 cars = ["bmw", "audi", "toyota", "subaru"] print(cars) cars.reverse() print(cars) 输出为: ['bmw', 'audi', 'toyota', 'subaru'] ['subaru', 'toyota', 'audi', 'bmw']…
# -*- coding: GBK -*- liebiao = ["zhang", "li", "wang", "zhou"] print("wo yao qing :" + liebiao[0] + "." + liebiao[1] + "." + liebiao[2] + "." + liebiao[3]) 输出为: wo yao qing :…
motorcycles = ["honda", "yamaha", "suzuki", "ducati"] print(motorcycles) motorcycles.remove("ducati") print(motorcycles) 输出为: ['honda', 'yamaha', 'suzuki', 'ducati'] ['honda', 'yamaha', 'suzuki'] 2. motorc…
motorcycles = ["honda", "yamaha", "suzuki"] first_owned = motorcycles.pop(0) print("The first motorcycle I owned was a " + first_owned.title() + ".") 输出为: The first motorcycle I owned was a Honda.…
motorcycle = ["honda", "yamaha", "suzuki"] last_owned = motorcycle.pop() print("The last motorcycle I owned was a " + last_owned.title() + ".") 输出为: The last motorcycle I owned was a Suzuki.…
motorcycles = ["honda", "yamaha", "suzuki"] print(motorcycles) del motorcycles[1] print(motorcycles) 输出为: ['honda', 'yamaha', 'suzuki'] ['honda', 'suzuki']…