python中列表的基本操作
来源:互联网 发布:酒瓶设计软件 编辑:程序博客网 时间:2024/06/09 16:58
#encoding:utf-8# 访问列表元素print("\n访问列表元素:")bicycles = ['terk', 'cannonddale', 'redline', 'specialized']print(bicycles)print(bicycles[0])print(bicycles[3])print(bicycles[-1]) #输出最后一个元素print(bicycles[-2]) #输出倒数第二个元素,以此类推#在列表的末端添加元素print("\n在列表的末端添加元素:")motorcycles = ['honda', 'yamaha', 'suzuki']motorcycles.append('ducati')print(motorcycles)#在列表中插入元素print("\n在列表中插入元素:")motorcycles.insert(0, 'shihao')print(motorcycles)#在列表中删除元素print("\n在列表中删除元素;")del motorcycles[0]print(motorcycles)#弹出列表尾部的元素print("\n出列表尾部的元素:")pop_end = motorcycles.pop()print(motorcycles)print(pop_end)#弹出任意位置的元素print("\n弹出任意位置的元素;")pop_value = motorcycles.pop(1)print(motorcycles)print(pop_value)#根据值删除元素print("\n根据值删除元素:")motorcycles.remove('honda')print(motorcycles)#使用方法sort对列表进行永久性排序print("\n使用方法sort对列表进行永久性排序:")cars = ['bmw', 'audi', 'toyota', 'subaru']cars.sort() # 按字母排序print(cars)#按照字母顺序相反的永久性排序print("按照字母顺序相反的永久性排序:")cars.sort(reverse = True)print(cars)#使用函数sorted()对列表进行临时排序print("\n使用函数sorted()对列表进行临时排序:")print(sorted(cars)) #正序print(sorted(cars, reverse=True)) #逆序print("\n永久性反转列表的排列顺序:")cars.reverse()print(cars)print("\n确定列表的长度:")print(len(cars))print("\n用for循环来打印列表:")magicians = ['alice', 'david', 'carolina']for temp in magicians: print(temp)print("使用函数range():")for value in range(1, 5): print(value) #打印 1 2 3 4#将一系列数字转成列表numbers = list(range(1, 5))print(numbers)# ** 表示乘方运算squares = []for value in range(1, 5): squares.append(value**2)print(squares)#简单的统计计算print("\n简单的统计运算")print(min(squares)) #求最小值print(max(squares)) #求最大值print(sum(squares)) #求和#列表解析squares = [value**2 for value in range(1, 11)]print(squares)#使用列表的一部分print("\n\n\n切片:")players = ['charles', 'martina', 'michael', 'florence', 'eli']print(players[1:3])print("\n输出所有队员:")print(players[0:])print("\n输出最后三名队员:")print(players[-3:])#复制整个列表print("\n错误的复制方法:")my_foods = ['pizza', 'falafel', 'carrot cake']friend_foods = my_foodsmy_foods.append('cannoli')print(my_foods)print(friend_foods)print("\n正确的复制方法:")friend_foods = my_foods[:] #省略起始索引和终止索引my_foods.append('ice cream')print(my_foods)print(friend_foods)#元组:不可变的列表称为元组print("\n\n\n元组:")#定义元组edrtgrjjdimensions = (200, 50) #用小括号print(dimensions[0])print(dimensions[1])#虽然元组的元素不能修改,但可以重新定义整个元组dimensions = (400, 100)for dimension in dimensions: print(dimension)
阅读全文
1 0
- Python中字符串、列表的基本操作
- python中列表的基本操作
- python中列表(list)的基本操作
- python列表基本操作
- Python中列表的操作
- 列表的基本操作 of Python
- python中列表list的基本使用
- Python列表基本增减操作
- 列表的基本操作
- Python中列表常用的操作符
- python中列表的常见操作
- python中列表的操作与应用
- Python 序列之列表的独有的基本操作
- python的基本操作(列表,元组)
- Python列表、字典、元组的一些基本操作
- python基础知识之改变列表的基本操作
- python中的列表,元祖,字典,等基本数据类型的操作
- python 中string的基本操作
- shell基础命令
- 线程安全及解决机制简介
- oAuth2 feign 授权模式
- shell 展开
- 拆分窗口
- python中列表的基本操作
- Wind River workbench介绍
- org.springframework.web.SpringServletContainerInitializer cannot be cast to javax.servlet.ServletCon
- HDU 4009-Transfer water
- Java数据库面试题
- shell进程管理
- 谷歌验证码的使用
- TeX中的引号
- 第六章 面向对象(下)