第三章--列表简介

##3.1 列表是什么

  • 列表由一系列特定顺序的排列元素组成
  • 其中的元素之间可以没有任何关系
bicycles = ['trek','cannonable','redline','specialized']
print(bicycles)
  • 结果为
    在这里插入图片描述

3.1.1 访问列表元素

[0,4)
bicycles = ['trek','cannonable','redline','specialized']
print(bicycles[0])
print(bicycles[1])
print(bicycles[3])
print(bycycles[-1]) 输出列表倒数第一个的元素
print(bycycles[-2]) 输出列表倒数第二个的元素
  • 结果 为
    在这里插入图片描述

3.1.2 索引从0开始而不是1

3.1.3 使用列表中的各个值

bicycles = ['trek','cannonable','redline','specialized']
message = "My first bicycle was a "+ bicycles[0].title() + "."
print(message)
  • 结果为
    在这里插入图片描述

3.2 修改、添加和删除元素

3.2.1 修改列表元素

motorcycles = ['honda','yamaha','suzuki']
print(motorcycles)

motorcycles[0] = 'ducati' 直接修改列表的第0位元素
print(motorcycles)
  • 结果为
    在这里插入图片描述

3.2.2 在列表中添加元素

motorcycles = ['honda','yamaha','suzuki']
print(motorcycles)

motorcycles.append('ducati') 在列表末尾的添加元素
print(motorcycles)
  • 结果为
    在这里插入图片描述
    1. 在列表中插入元素
motorcycles = ['honda','yamaha','suzuki']
print(motorcycles)

motorcycles.insert(0,'ducati' )  在列表第0位插入元素
print(motorcycles)
  • 结果为
    在这里插入图片描述

3.2.3 从列表中删除元素

    1. 使用 del 语句删除元素
motorcycles = ['honda','yamaha','suzuki']
print(motorcycles)

del motorcycles[0] 删除列表第0位元素
print(motorcycles)
  • 结果为
    在这里插入图片描述
    1. 使用方法pop()删除元素
motorcycles = ['honda','yamaha','suzuki']
print(motorcycles)

默认弹出列表最后一个元素
popped_motorcycles = motorcycles.pop()
print(motorcycles)
print(popped_motorcycles)
  • 结果为
    在这里插入图片描述
    1. 弹出列表中任何位置处的元素
motorcycles = ['honda','yamaha','suzuki']
print(motorcycles)

弹出列表指定的一个元素
popped_motorcycles = motorcycles.pop(1)
print(motorcycles)
print(popped_motorcycles)
  • 结果为
    在这里插入图片描述
    1. 根据值删除元素
motorcycles = ['honda','yamaha','suzuki']
print(motorcycles)

弹出列表搜索指定的一个元素
motorcycles.remove('honda')
print(motorcycles)

使用变量替代删除的值
remove_value= 'yamaha'
motorcycles.remove(remove_value)
print(motorcycles)
  • 结果为
    在这里插入图片描述

3.3 组织列表

3.3.1 使用方法 sort() 对列表进行永久性排序

cars = ['bmw','audi','toyoda','subaru']
根据单词首字母的大小从小到大排序
cars.sort() 
print(cars)

根据单词首字母的大小从大到小排序
cars.sort(reverse=True) 
print(cars)
  • 结果为
    在这里插入图片描述

3.3.2 使用函数sorted()对列表进行临时排序

cars = ['bmw','audi','toyoda','subaru']

print("Here is the original lsit:")
print(cars)

对cars列表进行临时的字母大小排序
print("\nHere is the sorted list:")
print(sorted(cars))

print("\nHere is the original list again")
print(cars)
  • 结果为在这里插入图片描述

3.3.3 到这打印列表

cars = ['bmw','audi','toyoda','subaru']

print("Here is the original lsit:")
print(cars)

print("\nHere is the reverse list:")
到着打印列表
cars.reverse()
print(cars)
  • 结果为
    在这里插入图片描述

3.3.4 确定列表的长度

cars = ['bmw','audi','toyoda','subaru']
计算列表长度
string_len=len(cars)
print(string_len)
  • 结果为
    在这里插入图片描述

3.4 避免列表是避免索引错误

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值