首页 > 编程语言 > 详细

[python3 - Basic] List

时间:2018-09-15 18:05:26      阅读:184      评论:0      收藏:0      [点我收藏+]

Attribute

1. len()

list = [1,2,3]
length = len(list)

 

Condition

1. 如果元素存在list中

list = [a, b, c]
str = a

if str in list:
  print("This string is in the list.")

2. 如果list不为空

list = []

if not list:
    print ("List is empty.")

 

Add

1. append() - 在尾部加element

list = [1,2,3]
list.append(4) # list=[1,2,3,4]

 2. extend([]) - 在list后面,添加多个element

list1 = [a‘,b‘,c]
list2 = list1.extend([d‘,e‘]) #list2 = [‘a‘,‘b‘,‘c‘,‘d‘,‘e‘]

 

Delete

1. del

list = [1,2,3]
del list[-1] #list = [1,2]

2. pop()

list = [1,2,3]
lastEle = list.pop() # lastEle = 3 

 

Get

1. index(value) - 查找list中的值,并返回其下标

list = [1,2,6]
idx = list.index(1) #0
idx = list.index(3) #ValueError: 3 is not in list

 

[python3 - Basic] List

原文:https://www.cnblogs.com/break-dawnn/p/8983580.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!