Python中的列表(添加二元列表、连接、查找、排序、反序、赋值、删除、分片操作、负索引、)

#_*_coding:UTF-8_*_
# 列表list_name=[element1,element2,element3...]
# 1.添加元素
# append(value)用户在List的尾部添加一个元素
# insert(index,value)第一个参数index为将要插入到列表中的元素指定索引位置,第二个参数value为要出插入的值
# 2.元素赋值
# list_name=[index]=vlaue(index从0开始)
# 3.删除元素
# remove(value)用于移除第一个匹配值,如果要删除的元素值不存在,Python将抛出ValueError异常
# del list_name[index]该语句将删除列表中指定索引位置所表示的元素
# 4.分片赋值(分片:从第一个索引到第二个索引,不包含第二个索引所指向的元素)
# list_name[m:n]
numbers=[0,6]
numbers[1:1]={1,2,3,4,5}
print numbers
#输出为:
#[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# 1.使用负索引访问列表元素(倒数第一个为-1,倒数第二个为-2,以此类推)
# 2.列表的分片处理
# 定义一个含有6个元素的列表
userList=['0001','0002','0003','0004','0005','0006']
# 获取索引为2,3,和4的元素值
subUser1=userList[2:5]
# 获取索引为-3和-2的元素值
subUser2=userList[-3:-1]
# 获取索引为0,1,和-1的元素值
subUser3=userList[0:-2]
print subUser1
print subUser2
print subUser3
# 输出
#['0003', '0004', '0005']
#['0004', '0005']
#['0001', '0002', '0003', '0004']
# 3.便利二元列表 
# lsit_name=[[element1,element2,...],[element3,element4,...],...]
# 定义列表1
userList1=['0001','0002','0003','0004']
# 定义列表2
userList2=['0005','0006','0007']
# 定义二元列表,元素有列表1和列表2组成
userList=[userList1,userList2]
print userList
print "userList[0][1]=",userList[0][1]
print "userList[1][0]=",userList[1][0]
print "userList[0][3]=",userList[0][3]
# 遍历
for i in range(len(userList)):
    for j in range(len(userList[i])):
        print 'userLiset['+str(i)+']['+str(j)+']=',userList[i][j]
# 输出
#[['0001', '0002', '0003', '0004'], ['0005', '0006', '0007']]
#userList[0][1]= 0002
#userList[1][0]= 0005
#userList[0][3]= 0004
#userLiset[0][0]= 0001  便利整个二元列表(一共7个元素)
#userLiset[0][1]= 0002
#userLiset[0][2]= 0003
#userLiset[0][3]= 0004
#userLiset[1][0]= 0005
#userLiset[1][1]= 0006
#userLiset[1][2]= 0007
#如果访问的二元列表中的元素不存在,比如访问userList[1][3],而userList列表中的二个元素['0005','0006','0007']中不存在索引为3的位置,即会抛出错误信息IndexError:userList index out of range.
# 4.列表的链接
# 两种方式:a)调用extend()方法链接两个不同的列表,b)是使用运算符+、+=、或*.
userList1=['0001','0002','0003','0004']
userList2=['005','0006','0007']
userList1.extend(userList2)
print userList1
# 输出
#['0001', '0002', '0003', '0004', '005', '0006', '0007']
userList3=['0008','0009','0010']
userList4=['0011','0012','0013']
userList5=userList3+userList4
print userList5
userList5+=['0014']
userList6=['0015','0016']*2
print userList5
print userList6
# 输出
# ['0008', '0009', '0010', '0011', '0012', '0013']
# ['0008', '0009', '0010', '0011', '0012', '0013', '0014']
# ['0015', '0016', '0015', '0016']
# 1.列表的查找
# 通过两种方式来查找List中的元素:a)使用index()方法返回元素在列表中的位置,index()方法用于从列表中找到某个值的第一匹配项的索引位置;b)通过保留字in来判断是否在列表中。
userList=['0001','0002','0003','0004','0005','0006']
print '0002 location in :',userList.index('0002')
print '0002' in userList
# 输出
# 0002 location in : 1
# True
# 2.列表的排序
# 使用List中的Sort方法
userList=['0001','0004','0006','0002','0005','0003']
userList.sort() #正序
print userList
userList.sort(reverse=True) #逆序
print userList
#输出
#['0001', '0002', '0003', '0004', '0005', '0006']
#['0006', '0005', '0004', '0003', '0002', '0001']
# 3.列表的反转
# reverse()方法将列表中的元素反向存放
userList=['0001','0004','0006','0002','0005','0003']
userList.reverse()
print userList
#输出
#['0003', '0005', '0002', '0006', '0004', '0001']

# 用列表实现堆栈
# 使用列表的append和pop方法可以模拟这两种数据结构
# pop()方法会移除列表中的元素(默认为最后一个),并且返回该院素的值。
# pop()方法是唯一一个既能修改列表又能返回元素值(除了None值)的列表操作方法。
userList=['0001','0004','0006','0002','0005','0003']
# 使用pop()方法,移除列表最后一个元素(默认)
userList.pop()
print userList
# 继续移除列表中的第一个原色,即索引为0的元素
print userList.pop(0)
print userList
# 输出
# ['0001', '0004', '0006', '0002', '0005']
# 0001
# ['0004', '0006', '0002', '0005']
# 定义一个列表
userList=['0001','0002','0003','0004']
# 向列表中添加一个元素为0005
userList.append('0005')
print 'The sequence of list is :'
for item in range(len(userList)):
    print userList[item]
print 'The sequence of list is :'
for item in range(len(userList)):
    print userList.pop()  # 调用pop()方法一次从上到下弹出元素
# 输出
#The sequence of list is :
#0001
#0002
#0003
#0004
#0005
#The sequence of list is :
#0005
#0004
#0003
#0002
#0001


  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值