python中列表的相关内容

列表的创建

lst = ['hello', 'world', 98]
# 列表不同类型可以混存
# 通过索引定位
print(lst[0])
# 通过索引逆序定位
print(lst[-3])
print(id(lst))
print(type(lst))
print(lst)
# 第二类创建列表的方法,使用内置函数list()
lst2 = list(['hello', 'world', 98])
print(lst2)
# 列表的查询操作
# 获取列表中指定元素的索引,使用index()

lst = ['hello', 'world', 98, 'hello']
print(lst.index('hello'))  # 结果为0,因为查询列表中存在N个相同的元素,只返回相同元素中的第一个元素的索引
# print(lst.index('python'))  # 会抛出错误,ValueError: 'python' is not in list
# print(lst.index('hello', 1, 3))  # 意思是在索引1到3的位置上查找hello,但不包括3,会报错ValueError: 'hello' is not in list

# 在指定的范围内查找
print(lst.index('hello', 1, 4))  # 输出3,
# 获取列表中的单个元素
lst = ['hello', 'world', 98, 'hello','2344']
# 正向索引为0到N-1,逆向索引是-N到-1
print(lst[0])
print(lst[-5])


#  列表元素的判断
print('y' in 'python')# True
print('k' not in 'python')# True

print('y' in 'python')# True


lst = [10,20,'python']

print(10 in lst)# True



# 列表元素的遍历
for item in lst
print(item)
# 向列表的的末尾添加一个元素
lst = [10, 20, 30]
print('添加元素之前', lst)
lst.append(100)
print('添加元素之后', lst)

lst2 = ['hello','world']
lst.append(lst2)  # 将lst2作为一个元素作为一个元素 添加到lst后面,结果为[10, 20, 30, 100, ['hello', 'world']]
print(lst)

lst.extend(lst2) # [10, 20, 30, 100, ['hello', 'world'], 'hello', 'world']
print(lst)

# 在指定的位置上添加一个元素
lst.insert(1,90) # 在1的位置上添加90
print(lst)  # [10, 90, 20, 30, 100, ['hello', 'world'], 'hello', 'world']

# 切片
lst3 = ['True','False','world']
# 在任意位置上添加N多个元素
lst[1:] = lst3
print(lst)  # [10, 'True', 'False', 'world']
# 列表元素的删除
lst = [10, 20, 30, 40, 50, 60, 70]
lst.remove(30) # 从列表中移除一个元素,如果有重复元素只移除第一个元素
print(lst)  # [10, 20, 40, 50, 60, 70]

# pop()根据索引移除元素
lst.pop(1)
print(lst)  #将索引为1的元素移除

lst.pop()  # 如果不加参数,将删除列表中的最后一个参数
print(lst)


# 切片操作,删除至少一个元素,将产生一个新的列表对象
new_lst = lst[1:3]
print('原列表',lst)
print('新列表',new_lst)

# 不产生新列表对象,而是删除原列表中的内容
lst[1:3] = []
print(lst)  # 结果为[10, 60]


# 清楚列表中的所有元素
lst.clear()
print(lst)

'''del语句将列表对象删除'''
del lst
print(lst)
# 列表元素的修改

lst = [10,20,30,40,50,60]
lst[1]=100
print(lst)

lst[1:3] = [200,300,400,500]
print(lst)
# 列表的排序
lst = [20,40,10,98,54]
print('排序前的列表',lst)
# 开始排序,调用列表对象的sort方法,默认升序
lst.sort()
print('排序后的列表',lst)

# 通过关键字参数,将列表列表元素降序排序
lst.sort(reverse=True)
print(lst)
lst.sort(reverse=False)
print(lst)


print('--------------------------------------')

# 使用内置函数sorted对列表进行排序,将产生一个新的排序对象
lst=[20,40,10,98,54]
print('原列表',lst)
# 开始排序
new_list=sorted(lst)
print(new_list)
print('--------------------------------------')
# 指定关键字参数,实现列表元素的降序排列
desc_list=sorted(lst,reverse=True)
print(lst)
print(desc_list)

列表的生成式

# 列表的生成式
lst = [i for i in range(1,10)]
print(lst)

lst = [i*i for i in range(1,10)]
print(lst)
print('----------------------------------------------')
# 列表中元素的值为2,4,6,8,10
lst2 = [i*2 for i in range(1,6)]
print(lst2)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值