[python3 - Basic] List

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']
扫描二维码关注公众号,回复: 3206838 查看本文章

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

猜你喜欢

转载自www.cnblogs.com/break-dawnn/p/8983580.html
今日推荐