列表的增删改查


一、列表的增加

1、insert

a = [1, 2, 3, 4, 5, 6]
a.insert(1, 1.5)
print(a)

2、append

a = [1, 2, 3, 4, 5, 6]
a.append(7)
print(a)

3、extend

a = [1, 2, 3, 4, 5, 6]
a.extend([7, 8])
print(a)

二、列表的删除

1、remove

a = [1, 2, 3, 4, 5, 6]
a.remove(1)
print(a)

2、pop

a = [1, 2, 3, 4, 5, 6]
a.pop()
print(a)

b = [1, 2, 3, 4, 5, 6]
b.pop(-1)
print(b)

3、clear

a = [1, 2, 3, 4, 5, 6]
a.clear()
print(a)

4、del

a = [1, 2, 3, 4, 5, 6]
del a
print(a)

b = [1, 2, 3, 4, 5, 6]
del b[-1]
print(b)

三、列表的修改

a = [1, 2, 3, 4, 5, 6]
a[0] = 1.5
print(a)

四、列表的查询

1、in

a = [1, 2, 3, 4, 5, 6]
b = 1.5 in a
print(b)

2、index

a = [1, 2, 3, 4, 5, 6]
b = a.index(6)
print(b)

3、count

a = [1, 2, 1, 4, 5, 1]
b = a.count(1)
print(b)

results matching ""

    No results matching ""