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

python(列表)

时间:2018-04-20 23:36:46      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:效率   通过   赋值   元素   index   函数   extend   end   copy   

1.append函数

lst =[1,2,3]                        

lst.append(4)           

lst        

[1,2,3,4]     

2.clear函数

lst.clear()

lst

[] 等价于lst[0:4] = lst[:] =[]

3.copy

 a = [1,2,3] 

b = a.copy()

b[1] =4

a

[1,4,3]

4.extend函数(该函数比 ‘+‘ 效率高)

a = [1,2,3]

b = [4,5,6]

a.extend(b)

a

[1,2,3,4,5,6]

 

5.index函数

knights = [‘we‘,‘are‘,‘family‘]

knights.index(‘are‘)

1

 

6.insert(同extend,一样可以通过切片赋值来获得与insert一样的效果)

numbers = [1,2,3,4,5,6]

numbers.insert(3,‘four‘)

numbers

[1,2,3,‘four‘,5,6]

 

7.pop(从列表删除一个元素并返回这个元素)

x = [1,2,3]

x.pop()

3

x

[1,2]

 

python(列表)

标签:效率   通过   赋值   元素   index   函数   extend   end   copy   

原文地址:https://www.cnblogs.com/sdibtzhou/p/8893784.html

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