python列表笔记

# 列表:列表python中最基本的数据结构
# 序列中每个值都有对应的位置值
# python 有六个序列的内置类型,但是最常见的操作 索引 切片 加 乘 检查
# 列表是最常见的常用的python数据类型它可以作为一个方括号内的逗号分隔值出现
# 访问
list = ['red', 'green', 'blue', 'yellow', 'white', 'black']
print(list[0])
print(list[1])
print(list[2])
print("-------------------")
print(list[-1])
print(list[-2])
print(list[-3])
# 使用下标索引来访问列表中的值,同样你也可以使用方括号[]的形式截取字符
temp = list[0:4]
print(temp)  # ['red', 'green', 'blue', 'yellow']
# 列表中使用的是[a:b]  形式
temp = list[1:-2]
print(temp)
# append 在列表末尾添加新对象
list2 = [1, 2, 3, 4, "a", "b", 3, 3, "a"]
print(list2)
list2.append('traditional')
print(list2)
# count(obj) 统计某个元素在列表中出现的次数
print(list2.count(3))
# index() 函数用于从列表中找出某个值第一个匹配项的索引位置
# 语法: index()方法语句
print("3的索引位置", str(list2.index("a")))
# insert(index,obj) 用于将指定对象插入列表的指定位置 没有返回值
list2 = ["google", "run", "sun"]
list2.insert(1, "Baidu")
print(list2)
# pop()函数 用于移除列表中的一个元素,(默认最后一个元素)。并且返回该元素的值
# pop()方法语法
list2 = ["google", "run", "sun", "play", "dance", "sing", "sang"]
print(list2.pop(3))
print(list2)
# remove() 函数用于移除列表中某个值的第一个匹配项
list2.remove("google")
print(list2)
# reverse() 反向列表中的元素
list2.reverse()
print(list2)
# sort 排序
arr = [1, 4, 2, 6, 2, 4, 2]
arr.sort(reverse=True)
print(arr)
ar = [1, 4, 2, 6, 2, 4, 2]
print(sorted(ar))





 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

理想艺术!马

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值