[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']

 

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

 

posted @ 2018-09-15 17:20  break大蜗牛  阅读(111)  评论(0编辑  收藏  举报