python实现增删改查操作# 增加操作
# append在列表的尾部加⼊指定的元素
name_list = ["⼩明","⼩强","⼩红"]
name_list.append("⼩花")
print(name_list)
# extend将指定序列的元素依次追加到列表的尾部(合并),不会去重复内容
# name_list2 = ["⼩猪","⼩猫",'⼩明']
# d(name_list2)
# print(name_list)
#
# # " + "加法等价于 extend ⽅法
# name_list3=name_list+name_list2
# print(name_list3)
#
# # insert将指定的元素插⼊到对应的索引位上,注意负索引倒序插⼊,超过索引就会在末尾插⼊
# name_list.insert(0,"⼩尹")
# name_list.insert(-3,"⼩黄")
# print(name_list)
sortedlist
# 删除操作
# pop弹出,返回并删除指定索引位上的数据,默认删除索引为-1的数据(从右向左删除)
# name = name_list.pop(1)
# print(name)
# #指定索引删除
# print(name_list)
# del删除整个列表或列表的数据,del是python内置功能,不是列表独有的
# del name_list[0]
# print(name_list)
# remove删除⼀个指定的元素
# ve("⼩强")
# print(name_list)
# clear 清空列表
# name_list.clear()
# print(name_list)
# 修改操作
# name_list[0]="⼩⽩"
# print(name_list)
# 查操作
# 使⽤索引
name_list=["tom","jerry","array","tom"]
# name=name_list[2]
# print(name)
# index查,从左往右返回查到的第⼀个指定元素的索引,如果没有到,报错
# index=name_list.index("jerry")
# print(index)
# count计数,返回要计数的元素在列表当中的个数
num=unt("tom")
print(num)
# 默认排序
# lst1=["abd","ABD","aBe"]
# lst1.sort()
# print(lst1)
# 指定排序规则
# lst1=["abd","ABD","aBc"]
# lst1.sort(key=str.lower)
# print(lst1)
# # reverse()翻转
# lst1=["abc","ABD","aBe"]
# verse()
# print(lst1) # ["aBe","ABD","abc"]
# lst1=["abc","ABD","aBe"]
# print(lst1)
# lst2=sorted(lst1)
# print(lst2) # ["ABD","aBe","abc"]
# lst3=sorted(lst1,key=str.lower)
# print(lst3) # ["abc","ABD","aBe"]
# lst4=sorted(lst1,reverse=True)
# print(lst4) # ["abc","aBe","ABD"]

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。