python基础之操作列表
2024-09-06 09:45:15
遍历元素
magicians = ['alice','david','carolina']
for magician in magicians:
print(magician)
magicians = ['alice','david','carolina']
for magician in magicians:
print(magician.title() + ", that was a great trick")
print("Thank you, everyone. That was a great magic show")
遍历数字
range()生成一系列数字
for value in range(1,5):
print(value)
创建数字列表
number = list(range(1,6))
print(number)
number = list(range(2,11,2)) #创建从2开始相差为2的数字并不超过11
print(number)
squares = []
for value in range(1,11):
square = value ** 2 #平方数
squares.append(square)
print(squares)
数字列表统计
digits = [1,2,3,4,5,6,7,8,9,0]
print(min(digits)) #最小值
print(max(digits)) #最大值
print(sum(digits)) #求和值
列表解析
squares = [value**2 for value in range(1,11)]
print(squares)
切片
注意:数字是从0开始数
players = ['charles','martina','michael','florence','eli']
print(players[0:3]) #从第一个到第三个
players = ['charles','martina','michael','florence','eli']
print(players[:3]) #从第一个到第三个
players = ['charles','martina','michael','florence','eli']
print(players[2:]) #从第三个到最后一个
players = ['charles','martina','michael','florence','eli']
print(players[-3:]) #输出最后三个数字
url = list("0123456789")
print(url[0:10:2]) # 指定正序的间隔为2
print(url[-1:-10:-2]) #指定倒序的间隔为2
print(url[::-1]) # 倒序
print(url[-3:-1]) # 使用负数切片
遍历切片
players = ['charles','martina','michael','florence','eli']
for player in players[:3]:
print(player)
复制列表
my_foods = ['pizza','falafel','carrot cake']
friend_foods = my_foods[:]
my_foods.append('cannoli')
friend_foods.append('ice cream')
print(my_foods)
print(friend_foods)
注意:
my_foods = ['pizza','falafel','carrot cake']
friend_foods = my_foods #不能这样直接赋值
my_foods.append('cannoli')
friend_foods.append('ice cream')
print(my_foods)
print(friend_foods)
遍历元组
元组是不能修改的列表,因此不能随意更改元组里的元素
dimensions = (200,50)
for dimension in dimensions:
print(dimension)
元组赋值
虽然不能修改元组的元素,但可以给元组的变量赋值
dimensions = (200,50)
for dimension in dimensions:
print(dimension)
dimensions = (400,100)
for dimension in dimensions:
print(dimension)
最新文章
- iOS开发之Bug(持续更新)
- PostgreSQL-function、trigger
- iOS学习12之OC属性和点语法
- bzoj3730:震波
- CentOS 7一些常用配置
- VIew中的触摸事件 touchBegin 等一系列方法
- ibatis框架文件配置
- h2database源码浅析:MVTable与MVIndex
- 【JS】Intermediate9:jQuery: Other Tricks
- 用urlencode(String str)对URL传递参数进行编码,提高安全
- C/C++ 知识点---数组与指针
- 63、django之模版层(template)
- 笔记:Spring Cloud Eureka 服务治理
- Java程序员的Golang入门指南(上)
- android混淆那些坑
- ENQUIRE the predecessor to the World Wide Web.
- FlexItem 多行测试
- 面试总结之PYTHON
- STS导入Gradle项目出现 Could not create task of type 'DependencyManagementReportTask'
- golang管道
热门文章
- Java安全之安全加密算法
- 02 . Go框架之Gin框架从入门到熟悉(数据解析和绑定,渲染,重定向,同步异步,中间件)
- 购买GPRS DTU时应该怎么选择
- Deployment YAML方式创建Nginx服务
- re模块,判断某行/某字符是否存在
- IDEA 搭建 Spark 源码 (Ubuntu)
- 解决SBT下载慢,dump project structure from sbt?
- Miller-Rabin 素数检验算法
- .netcore跨域设置
- Docker(11)- docker ps 命令详解