[转载] python随笔2(列表的增删改查)

参考链接: Python中list的方法 2| del, remove(), sort(), insert(), pop(), extend()…

2.1列表

 列表由一系列按特定顺序排列的元素组成。在python中,用 [] 来表示列表,并用逗号分隔其中的元素。下面是一个简单的列表示例:

 

  items = ['a','b','b','d']

print(items)

 

 如果你让python将列表打印出来,python将打印列表的内部表示,包括方括号:

 

  ['a','b','c','d']

 

 访问列表元素,由于列表是有序集合,因此要访问列表的任何元素,只需将该元素的位置或索引告诉python即可。

 

  items = ['a','b','c','d']

print(items[0])

 

>>>a

 

 索引从0开始而不是1开始,python为访问最后一个元素提供了一个特殊的语法。将索引指定为-1,可让python返回最后一个列表元素。

 

  items = ['a','b','c','d']

print(items[-1])

 

>>>d

 

 可以用在你不知道列表长度的情况下访问最后的元素。这种约定也适用于其他负数索引,例如,索引-2返回倒数第二个列表元素,索引-3返回倒数第三个列表元素…

 2.2修改,添加和删除元素

 修改列表可指定列表名和要修改的元素的索引,再指定该元素的新值。

 

  motorcycles = ['honda','yamaha','suzuki']

print(motorcycles)

motorcycles[0] = 'ducati'

print(motorcycles)

  

  >>>['honda','yamaha','suzuki']

  >>>['ducati','yamaha','suzuki']

 

 

 

 你可以修改任何列表元素的值,而不仅仅是第一个。

 在列表末尾添加新元素

 

  motorcycles = ['honda','yamaha','suzuki']

motorcycles.append('ducati')

print(motorcycles)

 

>>>['honda','yamaha','suzuki','ducati']

 

 方法append()将元素’ducati’添加到了列表末尾。方法append()让动态创建列表变得简单,你可以先创建一个空列表,再使用一系列的append()语句添加元素。

 

  motorcycles = []

motorcycles.append('honda')

motorcycles.append('yamaha')

motorcycles.append('suzuki')

print(motorcycles)

 

>>>['honda','yamaha','suzuki']

 

 在列表中插入元素

 使用方法insert()可在列表的任何位置添加新元素。为此,你需要指定新元素的索引和值。

 

  motorcycles = ['honda','yamaha','suzuki']

 

motorcycles.insert(0,'ducati')

print(motorcycles)

 

>>>['ducati','honda','yamaha','suzuki']

 

 从列表中删除元素

 你可以根据位置或值来删除列表中的元素

 使用del语句删除元素

 (如果知道要删除的元素在列表中的位置,可使用del语句)

 

  motorcycles = ['honda','yamaha','suzuki']

prInt(motorcycles)

 

del motorcycles[0]

print(motorcycles)

 

>>>['honda','yamaha','suzuki']

>>>['yamaha','suzuki']

 

 有时候,你要将元素列表中删除,并接着使用它的值。方法pop()可删除列表末尾的元素,并让你能够接着使用它。pop(弹出)就源自于这样的类比:列表就像一个栈,而删除列表末尾的元素相当于弹出栈顶元素。

 

  motorcycles = ['honda','yamaha','suzuki']

print(motorcycles)

 

popped_motorcycles = motorcycles.pop()

print(motorcycles)

print(popped_motorcycles)

 

>>>['honda','yamaha','suzuki']

>>>['honda','yamaha']

>>>suzuki

 

 你可以使用pop()来删除列表中任何位置的元素,只要在括号中指定要删除的元素的索引即可。pop()默认删除最后一个。

 

  motorcycles = ['honda','yamaha','suzuki']

frist_owned = motorcycles.pop(0)

print('The first motorcycle I owned was a'+ frist_owned.title() + '.')

 

>>>The first motorcycle I owned was a Honda.

 

 如果你不确定该使用del语句还是pop()方法,下面是一个最简单的判断标准:如果你要从列表中删除一个元素,且不再以任何方式使用它,就是用del语句:如果你要在删除元素后还能继续使用它,就用pop()方法。

 根据值删除元素

 有时候,你不知道要从列表中删除的值在什么位置,你可以使用方法remove()。

 例如,假设我们要从列表motorcycles中删除值’ducati’

 

  motorcycles = ['honda','yamaha','suzuki','ducati']

print(motorcycles)

moorcycles.remove('ducati')

print(motorcycles)

 

>>>['honda','yamaha','suzuki','ducati']

>>>['honda','yamaha','suzuki']

 

 使用remove()从列表中删除元素时,也可以直接使用它的值。

 

  motorcycles =  ['honda','yamaha','suzuki','ducati']

print(motorcycles)

too_expensive = 'ducati'

motorcycles.remove(too_expensive)

print(motorcycles)

print("\nA" + too_expensive.title() + "is too expensive for me.")

 

>>> ['honda','yamaha','suzuki','ducati']

>>> ['honda','yamaha','suzuki']

>>>A Ducati is too expensive for me.

 

 方法remove()只删除第一个指定的值。如果要删除的值可能在列表中出现多次,就需要使用循环来判断是否删除了所有这样的值。

 2.3组织列表

 在创建列表时,元素的排列顺序常常无法预测。因为你并非总能控制用户输入的顺序。但你经常需要以特定顺序呈现信息。

 使用方法sort()对列表进行永久排序

 

  cars = ['bmw','audi','toyota','subaru']

cars.sort()

print(cars)

 

>>>['audi','bme','subaru','toyota']

 

 方法sort()永久性地修改了列表元素的顺序

 你还可以按与字母顺序相反的顺序排序列表元素,只需向sort()方法传递参数 reverse = True 

 

  cars = ['bmw','audi','toyota','subaru']

cars.sort(reverse = True)

print(cars)

 

>>>['toyota','subaru','bmw','audi']

 

 使用函数sorted()对列表进行临时排列

 要保留列表元素原来的排列顺序,同时以特定的顺序呈现它们,可使用函数sorted()。函数sorted()可让你能够按特定顺序显示列表元素,同时不影响它们在列表中的原始排列顺序。

 

  cars = ['bmw','audi','toyota','subaru']

print(cars)

print(sorted(cars))

print(cars)

 

>>>['bmw','audi','toyota','subaru']

>>>['audi','bmw','subaru','toyota']

>>>['bmw','audi','toyota','subaru']

 

 调用sorted()后,列表元素的排列顺序并没有改变。如果你要按与字母顺序相反的顺序显示列表,也可以向函数sorted()传递参数 reverse = True。

 倒着打印列表

 要反转列表元素的排列顺序,可用方法reverse()。

 

  cars = ['bmw','audi','toyota','subaru']

print(cars)

 

cars.reverse()

print(cars)

 

>>>['bmw','audi','toyota','subaru']

>>>['subaru','toyota','audi','bmw']

 

 reverse()只是反转列表元素的排列顺序。如果想要恢复到原来的顺序,只需要再次对列表调用reverse()即可。

 确定列表的长度

 使用函数len()可以快速知道列表的长度。

 

  >>>cars = ['bmw','audi','toyota','subaru']

>>>len(cars)

 

4

 

 注意:Python计算列表元素数时从1开始。

 

转载于:https://www.cnblogs.com/wf1017/p/9143102.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值