python增删改查

2018-07-22  本文已影响0人  录崽

(一)、列表:

1、列表里的数据分为:可变类型数据 and不可变类型数据
可变类型数据有 : 数组、集合、字典
不可变类型数据有:字符串、元组、整数
2.列表的定义:

列表用中括号表示:
list = [ ]

3.列表的增 删 改 查:

索引从0开始

添加:append 、insert 、 extend

1.append(数据):把添加的元素放进列表里

list = []
list.append("老王")
print(list)

list = ["老王"]

2.insert(数据,索引):把添加的元素根据索引放在该索引的位置上

list = ["老王"]
list.insert(0,"老宋")
print(list)

list = ["老宋","老王"]

3.extend(列表):把一个列表的内容添加进另一个列表,依次放在后面

list = ["老张"]
list1 = ["老王","老宋"]
list.extend(list1)
print(list)

list = ["老张","老王","老宋"]

删除:remove ()、pop() 、pop(索引)

1.remove(列表里的数据):指定删除某个元素

list = ["老王","老宋","老张"]
list.remove("老宋")
print(list)

list = ["老王","老张"]

2.pop():默认删除最后一个

list = ["老王","老宋","老张"]
list.pop()
print(list)

list = ["老王","老宋"]

3.pop(索引):删除索引位置上的那个值

list = ["老王","老宋",]
list.pop(0)
print(list)

list = ["老宋"]

修改:list [索引] = 数据
list = ["老王","老宋","老张","老赵"]
list[1] = "老马"
print(list)

list = ["老王","老马","老张","老赵"]

查找:list [索引]
list = ["老王",`"老马"`,"老张","老赵"]
list [2]
print(list)

list = ["老张"]

上一篇下一篇

猜你喜欢

热点阅读