bicycles[-n]
意为访问列表bicycles的倒数第n个元素。
transportations = ['car' , 'bicycle' , 'motorcycle'] print(transportations) transportations[0] = 'train' print(transportations) 输出: ['car', 'bicycle', 'motorcycle'] ['train', 'bicycle', 'motorcycle']
1.在列表末尾添加元素,append: transportations = ['car' , 'bicycle' , 'motorcycle'] print(transportations) transportations.append('train') print(transportations) 输出: ['car', 'bicycle', 'motorcycle'] ['car', 'bicycle', 'motorcycle', 'train']
方法:可以先传建一个空列表,再使用一系列函数调用append()来添加元素,例如:
transportations = [] transportations.append('car') transportations.append('bicycle') transportations.append('motorcycle')
这种方法很常见,因为经常要等到程序运行后,我们才知道用户要在程序中存储哪些数据。为控制用户,可以首先创建一个空列表,用于存储用户将要输入的值,然后将用户提供的每个新值附加到列表中。
2.在列表中插入元素 在列表中任意位置添加新的元素,insert(): transportations = ['car' , 'bicycle' , 'motorcycle'] print(transportations) transportations.insert(1,'train') print(transportations) 输出: ['car', 'bicycle', 'motorcycle'] ['car', 'train', 'bicycle', 'motorcycle']
1.知道索引下的删除del,例如: transportations = ['car' , 'bicycle' , 'motorcycle'] print(transportations) del transportation[1] print(transportations) 输出: ['car', 'bicycle', 'motorcycle'] ['car', 'motorcycle']
当我们要将元素从列表中删除,并接着使用它的值,是可以用pop()方法删除列表末尾的的元素,并能够接着使用它。
transportations = ['car' , 'bicycle' , 'motorcycle'] print(transportations) popped_transportation = transportation.pop() print(popped_transportation) print(transportations) 输出: ['car', 'bicycle', 'motorcycle'] motorcycle ['car', 'bicycle']
事实上,若我们采用在pop()方法中指定参数的方式,可以删除列表中任意位置的元素,并接着使用。只需要在圆括号中指定要删除的元素索引即可,例如:
transportations = ['car' , 'bicycle' , 'motorcycle'] print(transportations) popped_transportation = transportations.pop(1) print(popped_transportation) print(transportations) 输出: ['car', 'bicycle', 'motorcycle'] bicycle ['car', 'motorcycle']
如果我们不知道要删除元素的索引,而知道元素的值,此时我们可以用remove的方法,例如:
transportations = ['car' , 'bicycle' , 'motorcycle'] print(transportations) transportations.remove('bicycle') print(transportations) 输出: ['car', 'bicycle', 'motorcycle'] ['car', 'motorcycle']
在上例中是采用参数直接传递的方式,当然,也可以通过变量来传递参数,例如:
transportations = ['car' , 'bicycle' , 'motorcycle'] print(transportations) whatiwanttodelete = 'bicycle' transportations.remove(whatiwanttodelete) print(transportations)
也可以达到同样的效果,这样做的好处是我们可以像使用pop()一样,接着使用这个被删除的元素,但需要注意的是,这二者的实现原理是完全不一样的!注意:remove()只能删除第一个指定的值!如果删除的值在列表中出现多次,就需要使用循环来确保将每个值都删除!
1.按照字母顺序排列,例如: transportations = ['car' , 'bicycle' , 'motorcycle'] print(transportations) transportations.sort() print(transportations) 输出: ['car', 'bicycle', 'motorcycle'] ['bicycle', 'car', 'motorcycle'] 2.按照与字母顺序相反的顺序排列元素,只需在使用sort()时传递参数reverse,使reverse = True即可 transportations = ['car' , 'bicycle' , 'motorcycle'] print(transportations) transportations.sort(reverse = True) print(transportations) 输出: ['car', 'bicycle', 'motorcycle'] ['motorcycle', 'car', 'bicycle']
注意:对列表的修改是永久性的!!!
使用方法sorted()对列表临时排序
transportations = ['car' , 'bicycle' , 'motorcycle'] print(transportations) print(sorted(transportations)) print(transportations) 输出: ['car', 'bicycle', 'motorcycle'] ['bicycle', 'car', 'motorcycle'] ['car', 'bicycle', 'motorcycle'] 若要按照与字母顺序相反的顺序排列元素,只需在使用sorted()时传递参数reverse,使reverse = True即可
可见,函数sorted()并没有改变列表元素的原始顺序,如果想通过sorted()永久改变列表,需要将调用sorted()后的列表传递给原始列表,如下:
transportations = sorted(transportations)
注意:在并非所有的值都是小写时,按字母顺序排列列表元素要复杂些。此时在决定排列顺序时,有多种解读大写字母的方式。
倒着打印列表元素reverse()
transportations = ['car' , 'bicycle' , 'motorcycle'] print(transportations) transportations.reverse() print(transportations) 输出: ['car', 'bicycle', 'motorcycle'] ['motorcycle', 'bicycle', 'car']
注意:reverse()不是按照字母顺序颠倒顺序,而只是将列表元素反转而已!!!如果想要恢复到原来的顺序只需要再次调用reverse即可。
确定列表长度len()
transportations = ['car' , 'bicycle' , 'motorcycle'] print(transportations) print(len(transportations)) 输出: ['car', 'bicycle', 'motorcycle'] 3