Python中使用列表的一部分——参考Python编程从入门到实践
处理列表中的部分元素——切片
1. 切片
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[0:3]) # 打印列表的切片,其中包含前三个元素,输出也是一个列表
print(players[1:4]) # 提取列表的第2、3、4个元素
print(players[:4]) # 没有指定索引,故打印出列表的前4个元素
print(players[2:]) # 没有指定终止索引,故打印出列表中第二个元素之后的所有元素(不包含第二个,由于索引从0开始)
print(players[-3:]) # 列表从右往左的索引从-1开始,故打印出列表的最后三个元素
print(players[:-2]) # 切片时不包含指定的第二个索引,故打印除了最后两个元素以外的其他元素
运行结果:
['charles', 'martina', 'michael']
['martina', 'michael', 'florence']
['charles', 'martina', 'michael', 'florence']
['michael', 'florence', 'eli']
['michael', 'florence', 'eli']
['charles', 'martina', 'michael']
2. 遍历切片
for player in players[:3]: # 遍历前3个元素
print(player.title())
运行结果:
Charles
Martina
Michael
3. 复制列表
my_foods = ['pizza', 'falafel', 'carrot cake'] # 定义列表
friend_foods = my_foods[:] # 复制列表
print('My favorite foods are:')
print(my_foods)
print("\nMy friend's favorite foods are:")
print(friend_foods)
运行结果:
My favorite foods are:
['pizza', 'falafel', 'carrot cake'] My friend's favorite foods are:
['pizza', 'falafel', 'carrot cake']
为每个列表添加一个元素:
my_foods.append('cannoli')
friend_foods.append('ice cream')
print('My favorite foods are:')
print(my_foods)
print("\nMy friend's favorite foods are:")
print(friend_foods)
运行结果:
My favorite foods are:
['pizza', 'falafel', 'carrot cake', 'cannoli'] My friend's favorite foods are:
['pizza', 'falafel', 'carrot cake', 'ice cream']
若将复制列表代码friend_foods = my_foods[:]用friend_foods = my_foods替代,运行上边添加元素代码会怎么呢?
My favorite foods are:
['pizza', 'falafel', 'carrot cake', 'cannoli', 'cannoli', 'ice cream'] My friend's favorite foods are:
['pizza', 'falafel', 'carrot cake', 'cannoli', 'cannoli', 'ice cream']
比较运行结果可知:代码friend_foods = my_foods只是将两个变量指向同一个列表;
Note:复制列表时有么有方括号将会得到两种不同的结果。
Python中使用列表的一部分——参考Python编程从入门到实践的更多相关文章
- julia与python中的列表解析.jl
julia与python中的列表解析.jl #=julia与python中的列表解析.jl 2016年3月16日 07:30:47 codegay julia是一门很年轻的科学计算语言 julia文档 ...
- Python中的列表解析和生成器表达式
Python中的列表解析和生成器表达式 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.列表解析案例 #!/usr/bin/env python #_*_coding:utf-8 ...
- Python中对列表排序实例
Python中对列表排序实例 发布时间:2015-01-04 09:01:50 投稿:junjie 这篇文章主要介绍了Python中对列表排序实例,本文给出了9个List的排序实例,需要的朋友可以参考 ...
- python中的列表及numpy数组排序
一.列表排序 # python中对列表排序有sort.sorted两种方法,其中sort是列表内置方法,其帮助文档如下:In [1]: help(sorted) Help on built-in f ...
- Python中的列表生成式和多层表达式
Python中的列表生成式和多层表达式 如何生成[1x1, 2x2, 3x3, ..., 10x10]的列表? L=[]; ,): L.append(x*x) print L print (" ...
- Python中的列表,元组,字符串之间的相互转化
Python中的列表元组和字符串之间的相互转化需要利用,tuple(),list(),str(). 示例如下: >>> the_string = "hello I'am x ...
- 逗号分隔的字符串转换为Python中的列表 split
将逗号分隔的字符串转换为Python中的列表 给定一个字符串: 它是由逗号分隔的几个值的序列: mStr = '192.168.1.1,192.168.1.2,192.168.1.3' 如何将字符 ...
- Python编程从入门到实践笔记——操作列表
Python编程从入门到实践笔记——操作列表 #coding=utf-8 magicians = ['alice','david','carolina'] #遍历整个列表 for magician i ...
- Python编程从入门到实践笔记——列表简介
Python编程从入门到实践笔记——列表简介 #coding=utf-8 #列表——我的理解等于C语言和Java中的数组 bicycles = ["trek","cann ...
随机推荐
- docker版本Mysql安装
docker部署mysql 1. 下载 [root@localhost my.Shells]# ./dockerStart.sh start or stop start Redirecting to ...
- com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-418, SQLSTATE=42610, SQLERRMC=null
写了一条sql,在db2数据库中可以执行,但是转换成mybatis的mapper文件后,在执行排序操作时报该错误. 我排序是这样写的 <if test="orderStr != nul ...
- FIS3
#npm install -g cnpm --registry=https://registry.npm.taobao.org#cnpm install -g fis3 npm install fis ...
- dump文件
https://blog.csdn.net/icandoit_2014/article/details/78739962 可以看出,此种方法只适用于程序崩溃但没有立即自行退出的情况.倘若程序故障后自行 ...
- currency
currency 美 ['kʌrənsi] 英 ['kʌrənsi] n.货币:通货:通用:流行 网络流通:货币型:币种
- linux设备树编译器DTC【转】
本文转载自:https://blog.csdn.net/chenmiaowu88/article/details/54342823 参考文件: \kernel\Documentation\device ...
- Spring boot+Vue全栈开发---Spring Boot文件上传
https://blog.csdn.net/Day_and_Night_2017/article/details/86980743 文件上传涉及到两个组件:CommonsMultipartResolv ...
- python+opencv图像变换的两种方法cv2.warpAffine和cv2.warpPerspective
本文链接:https://blog.csdn.net/qq_27261889/article/details/80720359 # usr/bin/env python # coding: utf- ...
- Sizes of integer types 整形字节长度 系统字节
/usr/include/limits.h /* Copyright (C) 1991, 1992, 1996, 1997, 1998, 1999, 2000, 2005 Free Software ...
- linux服务之dns
安装dig工具 [root@cu-app-107 ~]# cat /etc/redhat-releaseCentOS Linux release 7.5.1804 (Core) [root@cu-ap ...