Loading

数据结构之列表(Python)

数据结构之列表

1. 创建空列表

object_list = list()  # 或者 object_list = []
# 查看变量类型
print (type(object_list))
# 打印列表
print (object_list)

2. 列表添加元素

append()方法:在列表末尾添加新元素

object_list = list()
object_list.append ("钢铁侠")
print (object_list)

insert()方法:指定元素位置后插入新元素,插入位置及后面的元素自动往后移动

object_list = ["钢铁侠", "黑寡妇", "绿巨人"]
# 打印列表
print (object_list)
# 指定为位置插入新元素
if '黑寡妇' in object_list:
  idx = object_list.index ("黑寡妇")
  object_list.insert (idx, "绯红女巫")
# 打印更新后的列表
print (object_list)

extend()方法:添加一个可迭代对象(例如列表、元素、字典)

# 创建空列表
object_list = list()
# 打印列表
print (object_list)
# 添加可迭代对象
new_object = ["钢铁侠", "黑寡妇", "绿巨人"]
new_object2 = ('蚁人', '鹰眼', '雷神')
new_object3 = {"西游记":'唐僧', "水浒传":'宋江', "红楼梦":'林黛玉'}
object_list.extend (new_object)
object_list.extend (new_object2)
object_list.extend (new_object3.items())
# 打印更新后的列表
print (object_list)

3. 列表删除元素

pop()方法:按照指定下标索引值删除元素(注意:不能对空列表使用pop()方法,否则python会报错)

# 创建空列表
object_list = list()
# 添加元素
object_list.append ("钢铁侠")
object_list.append ("黑寡妇")
object_list.append ("绿巨人")
# 打印列表
print (object_list)

# 判断元素是否在列表中,然后删除该元素
if '绿巨人' in object_list:
  idx = object_list.index ("绿巨人")
  # 删除指定元素
  object_list.pop (idx)
# 打印删除元素后的列表
print (object_list)

remove()方法:按照指定元素名删除元素

# 创建空列表
object_list = list()
# 添加元素
object_list.append ("钢铁侠")
object_list.append ("黑寡妇")
object_list.append ("绿巨人")
# 打印列表
print (object_list)

# 判断元素是否在列表中,然后删除该元素
if '绿巨人' in object_list:
  # 删除指定元素
  object_list.remove ("绿巨人")
# 打印删除元素后的列表
print (object_list)

clear()方法:清空列表所有元素

object_list = ["钢铁侠", "黑寡妇", "绿巨人"]
# 打印列表
print (object_list)
# 清空列表所有元素
object_list.clear ()
# 打印更新后的列表
print (object_list)

4. 列表替换元素

# 创建空列表
object_list = list()
# 添加元素
object_list.append ("钢铁侠")
object_list.append ("黑寡妇")
object_list.append ("绿巨人")
# 打印列表
print (object_list)

# 替换指定元素
if '钢铁侠' in object_list:
  idx = object_list.index ("钢铁侠")
  object_list[idx] = "美国队长"
# 打印更新元素后的列表
print (object_list)

5. 列表查找元素

index()方法:index()方法表示在列表中查找第一个匹配目标对象的下标索引值

# 创建空列表
object_list = list()
# 添加元素
object_list.append ("钢铁侠")
object_list.append ("黑寡妇")
object_list.append ("绿巨人")
# 打印列表
print (object_list)

# 查找指定元素
if '黑寡妇' in object_list:
    idx = object_list.index ('黑寡妇')
    #打印元素及其所在列表位置
    print ("元素:{} 下标索引值:{}".format(object_list[idx], idx))

6. 统计列表元素数量

# 创建空列表
object_list = list()
# 添加元素
object_list.append ("钢铁侠")
object_list.append ("黑寡妇")
object_list.append ("绿巨人")
# 打印列表
print (object_list)

# 统计列表元素数量
list_num = len (object_list)
print (list_num)

7. 判断列表是否存在某元素

# 创建空列表
object_list = list()
# 添加元素
object_list.append ("钢铁侠")
object_list.append ("黑寡妇")
object_list.append ("绿巨人")
# 打印列表
print (object_list)

# 判断列表是否存在某元素
targe = "黑寡妇"
if targe in object_list:
    print ("列表存在元素:{}".format(targe))
else:
    print ("列表不存在元素:{}".format(targe))

8. 列表的合并

# 创建空列表
object_list = list()
# 打印列表
print (object_list)
# 添加可迭代对象
new_object = ["钢铁侠", "黑寡妇", "绿巨人"]
new_object2 = ['蚁人', '鹰眼', '雷神']
# 列表进行加法运算
object_list = new_object + new_object2
# 打印更新后的列表
print (object_list)

posted @ 2022-05-17 16:06  eiSouthBoy  阅读(112)  评论(0)    收藏  举报