Python列表使用

列表

:

列表的新增\修改\删除操作*

在这里插入图片描述

index()

列表的index函数用于获取指定元素的索引值

list = ['张三', '赵六', '李四', '王五', '赵六', '钱七', '孙八']

zhaoliu_index = list.index('赵六')
print(zhaoliu_index)

**结果**
1

找到指定元素,并且显示元素索引位置

for遍历

在这里插入图片描述

while遍历

在这里插入图片描述

reverse()

#列表的反转与排序
persons = ['张三', '赵六', '李四', '王五', '赵六', '钱七', '孙八']
persons.reverse() #reverse方法用于反转列表
print(persons)

numbers = [28,32,14,12,53,42]
numbers.sort(reverse=True) #sort()用于排序,reverse=True代表降序排列
print(numbers)


**结果:**
	['孙八', '钱七', '赵六', '王五', '李四', '赵六', '张三']
	[53, 42, 32, 28, 14, 12]

append()

# 列表的写操作
persons = ['张三', '赵六', '李四', '王五', '赵六', '钱七', '孙八']
# 列表的追加
persons.append("杨九")
print(persons)

**结果**
['张三', '赵六', '李四', '王五', '赵六', '钱七', '孙八', '杨九']

insert()

# 列表的插入
persons = ['张三', '赵六', '李四', '王五', '赵六', '钱七', '孙八', '杨九']
persons.insert(2 , '刘二')
print(persons)

**结果**
['张三', '赵六', '刘二', '李四', '王五', '赵六', '钱七', '孙八', '杨九']
___________________________________________________________________________
persons.insert(len(persons) , '候大') #在列表最后插入
print(persons)

**结果**
['张三', '赵六', '刘二', '李四', '王五', '赵六', '钱七', '孙八', '杨九', '候大']

列表更新

persons = ['张三', '赵六', '刘二', '李四', '王五', '赵六', '钱七', '孙八', '杨九', '候大']
persons[2] = '宋二'
print(persons)

**结果**
['张三', '赵六', '宋二', '李四', '王五', '赵六', '钱七', '孙八', '杨九', '候大']
_______________________________________________________________________________
# 列表范围取值是"左闭右开"
persons[3:5] = ['王五','李四']
print(persons)

**结果**
['张三', '赵六', '宋二', '王五', '李四', '赵六', '钱七', '孙八', '杨九', '候大']

列表删除remove/pop

# 按元素内容删除
persons = ['张三', '赵六', '宋二', '王五', '李四', '赵六', '钱七', '孙八', '杨九', '候大']
persons.remove('宋二')
print(persons)

**结果**
['张三', '赵六', '王五', '李四', '赵六', '钱七', '孙八', '杨九', '候大']

_____________________________________________________________________________________
persons = ['张三', '赵六', '王五', '李四', '赵六', '钱七', '孙八', '杨九', '候大']
# 按索引值删除元素
persons.pop(4)
print(persons)

**结果**
['张三', '赵六', '王五', '李四', '钱七', '孙八', '杨九', '候大']

_________________________________________________________________________________________
# 范围删除
persons = ['张三', '赵六', '王五', '李四', '钱七', '孙八', '杨九', '候大']
persons[4:7] = []
print(persons)

**结果**
['张三', '赵六', '王五', '李四', '候大']


count()-统计出现次数

persons = ['张三', '赵六', '李四', '王五', '赵六', '钱七', '孙八'] 
#统计出现次数
cnt =persons.count(赵六)
print(cnt)

**结果**
2

追加操作append()/extend()

persons = ['张三', '赵六', '李四', '王五', '赵六', '钱七', '孙八'] 
#追加操作
#append将整个列表追加到末尾,extend则是将列表中的元素追加到原始列表末尾
persons.append(['杨九' ,'吴十'])
print(persons)

**结果**
['张三', '赵六', '李四', '王五', '赵六', '钱七', '孙八',['杨九','吴十']] 
___________________________________________________________________
persons.extend(['杨九' , '吴十'])
print(persons)

**结果**
['张三', '赵六', '李四', '王五', '赵六', '钱七', '孙八','杨九','吴十'] 

copy()函数用于复制列表

persons = ['张三', '赵六', '李四', '王五', '赵六', '钱七', '孙八'] 

persons1 = persons.copy()  #copy()是新重新创建一块内存
___________________________________________________________________
persons2 = persons #这样是指向同一块内存地址,persons做操作同步persons2
persons2.clear()
print(persons)

**结果**
[]
****************************************************************************************************
#is 身份运算符用于判断两个变量是否指向同一块内存
print(persons1 is persons) #Flase
print(persons2 is persons) #True

clear()用于清除列表

变量同上↑

persons.clear()
print(persons)

**结果**
[]
_____________________________
print(persons1)

**结果**
['张三', '赵六', '李四', '王五', '赵六', '钱七', '孙八'] 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值