Python编程:从入门到实践—列表
访问列表中的元素
>>> bicycles = ['trek','cannondale','redline','specialized']
>>> print(bicycles)
['trek', 'cannondale', 'redline', 'specialized']
>>> print(bicycles[0])
trek
>>> print(bicycles[-1])
specialized
>>> print(bicycles[0].title())
Trek
修改、添加和删除元素
修改列表元素
>>> motorcycles = ['honda','yamaha','suzuki']
>>> print(motorcycles)
['honda', 'yamaha', 'suzuki']
>>> motorcycles[0] = 'ducati'
>>> print(motorcycles)
['ducati', 'yamaha', 'suzuki']
在列表中添加元素
>>> motorcycles = []
>>> motorcycles.append('honda')
>>> motorcycles.append('yamaha')
>>> motorcycles.append('suzuki')
>>> print(motorcycles)
['honda', 'yamaha', 'suzuki']
在列表中插入元素
>>> motorcycles = ['honda','yamaha','suzuki']
>>> motorcycles.insert(0,'ducati')
>>> print(motorcycles)
['ducati', 'honda', 'yamaha', 'suzuki']
从列表中删除元素
>>> motorcycles = ['honda','yamaha','suzuki']
>>> print(motorcycles)
['honda', 'yamaha', 'suzuki']
>>> del motorcycles[0]
>>> print(motorcycles)
['yamaha', 'suzuki']
使用方法pop()删除元素
方法pop()删除列表末尾的元素,并可以使用它
>>> motorcycles = ['honda','yamaha','suzuki']
>>> popped_motorcycle = motorcycles.pop()
>>> print(motorcycles)
['honda', 'yamaha']
>>> print(popped_motorcycle)
suzuki
应用:
>>> motorcycles = ['honda','yamaha','suzuki']
>>> last_owned = motorcycles.pop()
>>> print("The last motorcycles I owned was a " + last_owned.title() + ".")
The last motorcycles I owned was a Suzuki.
弹出列表中任何位置处的元素:
>>> motorcycles = ['honda','yamaha','suzuki']
>>> first_owned = motorcycles.pop(0)
>>> print('The first motorcycle I owned was a ' + first_owned.title() + '.')
The first motorcycle I owned was a Honda.
>>> print(motorcycles)
['yamaha', 'suzuki']
每当使用pop(),被弹出的元素就不再在列表中了。
使用值删除元素
>>> motorcycles = ['honda','yamaha','suzuki','ducati']
>>> print(motorcycles)
['honda', 'yamaha', 'suzuki', 'ducati']
>>> motorcycles.remove('ducati')
>>> print(motorcycles)
['honda', 'yamaha', 'suzuki']
使用remove()从列表中删除的元素,也可以接着使用它的值。
>>> motorcycles = ['honda','yamaha','suzuki','ducati']
>>> print(motorcycles)
['honda', 'yamaha', 'suzuki', 'ducati']
>>> too_expensive = 'ducati'
>>> motorcycles.remove(too_expensive)
>>> print(motorcycles)
['honda', 'yamaha', 'suzuki']
>>> print("\nA " + too_expensive.title() + " is too expensive for me.")
A Ducati is too expensive for me.
方法remove()只删除第一个指定的值。如果要删除的值可能在列表中出现多次,就需要使用循环来判断是否删除了所有这样的值。
组织索引
>>> cars = ['bmw','audi','toyoda','subaru']
>>> print(cars)
['bmw', 'audi', 'toyoda', 'subaru']
>>> cars.sort()
>>> print(cars)
['audi', 'bmw', 'subaru', 'toyoda']
方法sort()永久性地修改了列表元素的排列顺序,cars按字母顺序排列
倒序排序方式:
>>> cars = ['bmw','audi','toyoda','subaru']
>>> cars.sort(reverse=True)
>>> print(cars)
['toyoda', 'subaru', 'bmw', 'audi']
使用函数sorted()对列表进行临时排序
>>> cars = ['bmw','audi','toyoda','subaru']
>>> print("Here is the original list:",cars)
Here is the original list: ['bmw', 'audi', 'toyoda', 'subaru']
>>> print("Here is the original list:\n",sorted(cars))
Here is the original list:
['audi', 'bmw', 'subaru', 'toyoda']
>>> print("\nHere is the original list:",cars)
Here is the original list: ['bmw', 'audi', 'toyoda', 'subaru']
倒着打印列表
>>> cars = ['bmw','audi','toyoda','subaru']
>>> print(cars)
['bmw', 'audi', 'toyoda', 'subaru']
>>> cars.reverse()
>>> print(cars)
['subaru', 'toyoda', 'audi', 'bmw']
方法reverse()永久性地修改列表元素的排列顺序
确定列表长度:
>>> cars = ['bmw','audi','toyoda','subaru']
>>> len(cars)
操作列表
遍历整个列表:
>>> magicians = ['alice','david','carolina']
>>> for magician in magicians:
... print(magician)
...
alice
david
carolina
>>> for magician in magicians:
... print(magician.title() + ",that was a great trick!")
...
Alice,that was a great trick!
David,that was a great trick!
Carolina,that was a great trick!
在for循环之后执行一些操作
在for循环后面,没有缩进的代码只执行一次,而不会重复执行,Python根据缩进代码行与前一个代码的关系
#!/usr/bin/env python
# -*- encoding:utf-8 -*-
magicians = ['alice','elon','carolian']
for magician in magicians:
print(magician.title() + ",that was a great trick!")
print("I can't wait to see your next trick," + magician.title() + ".\n")
print("Thank you,everyone.That was a great magic show!")
执行结果:
Alice,that was a great trick!
I can't wait to see your next trick,Alice.
Elon,that was a great trick!
I can't wait to see your next trick,Elon.
Carolian,that was a great trick!
I can't wait to see your next trick,Carolian.
Thank you,everyone.That was a great magic show!
创建数值列表:
>>> for value in range(1,5):
... print(value)
...
1
2
3
4
使用range()创建列表
将range()作为list()的参数,输出将为一个数字列表
>>> numbers = list(range(1,5))
>>> print(numbers)
[1, 2, 3, 4]
使用函数range()时,还可以指定步长,函数range()从2开始数,然后不断加2,直到达到或超过终值11
>>> even_numbers = list(range(2,11,2))
>>> print(even_numbers)
[2, 4, 6, 8, 10]
使用函数range()几乎能够创建任何需求的数字集,例如:创建一个列表,其中包含前10个整数的平方。
>>> squares = []
>>> for value in range(1,11):
... square = value ** 2
... squares.append(square)
... print(squares)
...
[1]
[1, 4]
[1, 4, 9]
[1, 4, 9, 16]
[1, 4, 9, 16, 25]
[1, 4, 9, 16, 25, 36]
[1, 4, 9, 16, 25, 36, 49]
[1, 4, 9, 16, 25, 36, 49, 64]
[1, 4, 9, 16, 25, 36, 49, 64, 81]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
为了代码简洁,可不使用临时变量square
>>> squares = []
>>> for value in range(1,11):
... squares.append(value**2)
... print(squares)
...
[1]
[1, 4]
[1, 4, 9]
[1, 4, 9, 16]
[1, 4, 9, 16, 25]
[1, 4, 9, 16, 25, 36]
[1, 4, 9, 16, 25, 36, 49]
[1, 4, 9, 16, 25, 36, 49, 64]
[1, 4, 9, 16, 25, 36, 49, 64, 81]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
对数字列表执行简单的统计计算
>>> digits = [1,2,3,4,5,6,7,8,9,0]
>>> min(digits)
0
>>> max(digits)
9
>>> sum(digits)
45
使用列表的一部分
切片
Python编程:从入门到实践—列表的更多相关文章
- Python编程从入门到实践笔记——操作列表
Python编程从入门到实践笔记——操作列表 #coding=utf-8 magicians = ['alice','david','carolina'] #遍历整个列表 for magician i ...
- Python编程从入门到实践笔记——列表简介
Python编程从入门到实践笔记——列表简介 #coding=utf-8 #列表——我的理解等于C语言和Java中的数组 bicycles = ["trek","cann ...
- Python编程从入门到实践笔记——文件
Python编程从入门到实践笔记——文件 #coding=gbk #Python编程从入门到实践笔记——文件 #10.1从文件中读取数据 #1.读取整个文件 file_name = 'pi_digit ...
- Python编程从入门到实践笔记——函数
Python编程从入门到实践笔记——函数 #coding=gbk #Python编程从入门到实践笔记——函数 #8.1定义函数 def 函数名(形参): # [缩进]注释+函数体 #1.向函数传递信息 ...
- Python编程从入门到实践笔记——用户输入和while循环
Python编程从入门到实践笔记——用户输入和while循环 #coding=utf-8 #函数input()让程序暂停运行,等待用户输入一些文本.得到用户的输入以后将其存储在一个变量中,方便后续使用 ...
- Python编程从入门到实践笔记——字典
Python编程从入门到实践笔记——字典 #coding=utf-8 #字典--放在{}中的键值对:跟json很像 #键和值之间用:分隔:键值对之间用,分隔 alien_0 = {'color':'g ...
- Python编程从入门到实践笔记——if语句
Python编程从入门到实践笔记——if语句 #coding=utf-8 cars=['bwm','audi','toyota','subaru','maserati'] bicycles = [&q ...
- 《Python编程从入门到实践》_第十章_文件和异常
读取整个文件 文件pi_digits.txt #文件pi_digits.txt 3.1415926535 8979323846 2643383279 下面的程序打开并读取整个文件,再将其内容显示到屏幕 ...
- #Python编程从入门到实践#第四章笔记
#Python编程从入门到实践#第四章笔记 操作列表 1.遍历列表 使用for循环,遍历values列表 for value in values: print(value) 2.数字列表 使 ...
随机推荐
- Linux0.11之初识Makefile/build.c
前言 Makefile对于从来没有接触过的人来说是相当别扭的(比如我),但它确实又是非常重要的,它描述了一个Image是如何形成的,理解它也许并不能帮我解决实际问题,而且编写Makefile的工作也许 ...
- JProfiler> ERROR: Invalid license key. Aborting.
用IDEA+Tomcat的方式打开JProfiler,出现错误 1,Event Log 出错 16:10 Application Server was not connected before run ...
- 异步IO\数据库\队列\缓存\RabbitMQ队列
本节内容 Gevent协程 Select\Poll\Epoll异步IO与事件驱动 Python连接Mysql数据库操作 RabbitMQ队列 Redis\Memcached缓存 Paramiko SS ...
- 自我笔记,Rides介绍
Redis是一个key-value存储系统,和Memccached类似,支持存储的value类型相对更多,很大程度上补偿memcached这类key-value存储的不足 他提供了Java,c/c++ ...
- k3 cloud中单据体中文本自适应
在单据体中添加多行文本,然后设置本地配置,只读单元格自动换行
- .net 敏捷开发框架7.0.3 旗舰版
联系QQ:1516462411 索取
- 雷电模拟器 v3.71绿色版
目录 1. 按 2. 介绍 3. 下载地址 1. 按 安卓模拟器目前的市场基本上稳定了,逍遥.夜神.雷电,用的人都很多,网易mumu从阴阳师时代就被大家熟知,腾讯手游助手也是随着吃鸡游戏而火,这两个模 ...
- SQL SERVER BCP导入导出
bcp是SQL Server中负责导入导出数据的一个命令行工具,它是基于DB-Library的,并且能以并行的方式高效地导入导出大批量的数据.bcp可以将数据库的表或视图直接导出,也能通过SELECT ...
- 2014百度之星初赛第二场hdu 4831 Scenic Popularity
Scenic Popularity Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- GUI学习之二十三——QComboBox学习总结
我们在前面分别介绍了两种输入控件:纯键盘文本输入和步长调节器,下面我们来学习下组合框(下拉选择输入). 一.简介 1.下拉框是一个组合控件(包含一个文本显示控件和一个按钮).它默认显示最小的控件给用户 ...