Python-9- list of additions and deletions to change search

Foreword

This section is: list (list) CRUD. What is the list?

List (list) is the most commonly used data types Python, it can serve as a square brackets [Comma Separated Value] within appears. Such as: [1,5, "b"]

First, by

1, append increase

# . 1, the append by 
Li = [ ' XL ' , [. 3, 2,. 1], ' tiger ' , ' Aiya ' , ' lushen ' ] 
li.append ( ' the FPX ' )
 Print (Li)

 2, insert is inserted into the specified index

# 2, is inserted into the specified index INSERT 
Li = [ ' XL ' , [. 3, 2,. 1], ' tiger ' , ' Aiya ' , ' lushen ' ] 
li.insert (0, ' the FPX ' )
 Print (Li)

 3, extend, iterative elements, int iteration may fail

# 3, the element iteration, int iteration not 
Li = [ ' XL ' , [3, 2,. 1], ' tiger ' , ' Aiya ' , ' lushen ' ] 
li.extend ( ' dragon ' )
 Print (Li)

 Second, delete

1, pop deleted

# 1、pop 删除
li = ['XL', [3, 2, 1], '小龙', 'aiya', 'lushen']
name = li.pop(2)   # 有返回值
name1 = li.pop()    # 默认删除最后一个
print(name)
print(li)

 2、remove:按元素清除

# # 2、remove:按元素清除
li = ['XL', [3, 2, 1], '小龙', 'aiya', 'lushen']
li.remove('小龙')
print(li)

 3、clear:清空

# 3、clear:清空
li = ['XL', [3, 2, 1], '小龙', 'aiya', 'lushen']
li.clear()
print(li

4、del 切片删除

# 4、切片删除
# del li
li = ['XL', [3, 2, 1], '小龙', 'aiya', 'lushen']
del li[0:3]
print(li)

 三、改

1、下标直接赋值修改

# 1、下标直接赋值修改
li = ['XL', [3, 2, 1], '小龙', 'aiya', 'lushen']
li[0] = '男人'
print(li)

 2、切片会迭代处理,一般传列表能比较理想

# 2、切片会迭代处理
li = ['XL', [3, 2, 1], '小龙', 'aiya', 'lushen']
li[1:2] = '123456'
print(li)
# 传列表
li = ['XL', [3, 2, 1], '小龙', 'aiya', 'lushen']
li[1:3] = ['后裔', '鲁班']
print(li)

 

 四、查

li = ['XL', [3, 2, 1], '小龙', 'aiya', 'lushen']
for i in li:
    print(i)
print(li[0:2])

 五、其他操作

1、len()

2、count()

3、index()

QQ交流群:99941785

Guess you like

Origin www.cnblogs.com/gsxl/p/11962781.html