码迷,mamicode.com
首页 > 编程语言 > 详细

python切片

时间:2019-11-01 22:19:21      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:little   first   big   jin   增删改   list   倒数   reverse   fir   

增删改查

查 切片 []
print(a[1:])#取到最后
print(a[1:-1])#取到倒数第二值
print(a[1:-1:1])#从左到右一个一个去取
print(a[1::2])#从左到右隔一个去取
print(a[3::-1])
b=a[3::-1]
print(b)#[‘sanpang‘, ‘xiaohu‘, ‘jinxin‘, ‘wuchao‘]
print(a[-2::-1])
print(a[1:-1:-2])

添加 append insert

a.append(‘xuepeng‘) #默认插到最后一个位置
print(a)
a.insert(1,‘xuepeng‘) #将数据插入到任意一个位置
print(a)

修改

a[1]=‘haidilao‘
print(a)
a[1:3]=[‘a‘,‘b‘]
print(a)


删除 remove pop del

a.remove(a[0])
print(a)
b=a.pop(1)
print(a)
print(b)

del a[0]
print(a)
del a
print(a)
a.remove([‘wuchao‘,‘jinxin‘])
print(a)


count:计算某元素出现次数
t=[‘to‘, ‘be‘, ‘or‘, ‘not‘, ‘to‘, ‘be‘].count(‘to‘)
print(t)

extend
a = [1, 2, 3]
b = [4, 5, 6]
a.extend(b)
print(a)
print(b)

index # 根据内容找位置

a = [‘wuchao‘, ‘jinxin‘, ‘xiaohu‘,‘ligang‘, ‘sanpang‘, ‘ligang‘, [‘wuchao‘, ‘jinxin‘]]


first_lg_index = a.index("ligang")
print("first_lg_index",first_lg_index)
little_list = a[first_lg_index+1:]

second_lg_index = little_list.index("ligang")
print("second_lg_index",second_lg_index)

second_lg_index_in_big_list = first_lg_index + second_lg_index +1

print("second_lg_index_in_big_list",second_lg_index_in_big_list)
print("second lg:",a[second_lg_index_in_big_list])

reverse

a = [‘wuchao‘, ‘jinxin‘, ‘xiaohu‘,‘ligang‘, ‘sanpang‘, ‘ligang‘]
a.reverse()
print(a)

x = [4, 6, 2, 1, 7, 9]
x.sort(reverse=True)
print(x)#[1, 2, 4, 6, 7, 9]


a = [‘wuchao‘, ‘jinxin‘, ‘Xiaohu‘,‘Ligang‘, ‘sanpang‘, ‘ligang‘]
a.sort()
print(a)

print(a.count("haidilao ge"))
print(a.pop())
print(a)

python切片

标签:little   first   big   jin   增删改   list   倒数   reverse   fir   

原文地址:https://www.cnblogs.com/gerenboke/p/11779605.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!