深入解析Python小白学习【操作列表】

作者:130411422 时间:2023-02-18 03:48:07 

1.遍历列表

需要对列表中的每个元素都执行相同的操作时,可使用for 循环:


magicians = ['alice','david','carolina']
for magician in magicians:
 print(magician)
>>>alice
>>>david
>>>carolina

 循环中,Python将首先读取其中的第一行代码:


for magician in magicians:

这行代码让Python获取列表magicians 中的第一个值('alice' ),并将其存储到变量magician 中。接下来,Python读取下一行代码:


print(magician)

它让Python打印magician 的值——依然是'alice' 。鉴于该列表还包含其他值,Python返回到循环的第一行:


for magician in magicians:

Python获取列表中的下一个名字——'david' ,并将其存储到变量magician 中,再执行下面这行代码: 


print(magician)

以此类推,直至列表的最后一个元素。

对列表中的每个元素,都将执行循环指定的步骤,而不管列表包含多少个元素。如果列表包含一百万个元素,Python就重复执行指定的步骤一百万次,且通常速度非常快。 使用for 循环处理数据是一种对数据集执行整体操作的不错的方式。

2.避免缩进错误,Python根据缩进来判断代码行与前一个代码行的关系

2.1未缩进:


magicians = ['alice','david','carolina']
for magician in magicians:
print(magician)

IndentationError: expected an indented block

2.2循环后的冒号

 for 语句末尾的冒号告诉Python,下一行是循环的第一行。如果你不小心遗漏了冒号,将导致语法错误。

3.创建数值列表

3.1函数range()


for value in range(1,5):
 print(value)
>>>1
>>>2
>>>3
>>>4

函数range()让Python从你指定的第一个值开始数,在到达你指定的第二个值后停止,因此输出并不包含第二值。

3.2使用range()创建数字列表

将range() 作为list() 的参数,输出将为一个数字列表。


numbers = list(range(1,6))
print(numbers)
>>>[1, 2, 3, 4, 5]

range()函数还可指定步长:


even_numbers = list(range(1,13,2))
print(even_numbers)
>>>[1, 3, 5, 7, 9, 11]

函数range() 从1开始数,然后不断地加2,直到达到或超过终值。

使用函数range() 几乎能够创建任何需要的数字集。


squares = []
for value in range(1,11):
 squares.append(value**2)
print(squares)
>>>[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

4.列表解析

列表解析将for 循环和创建新元素的代码合并成一行,并自动附加新元素:


squares = [value**2 for value in range(1,11)]
print(squares)
>>>[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

首先,指定一个描述性的列表名,如squares。然后指定一个左方括号,并定义一个表达式,用于生成你要存储到列表中的值。在这个示例中,表达式为value**2 ,它计算平方值。接下来,编写一个for 循环,用于给表达式提供值,再加上右方括号。在这个示例中,for 循环为for value in range(1,11) ,它将值1~10提供给表达式value**2 。请注意,这里的for 语句末尾没有冒号。

5.列表切片(处理部分列表元素)

与range()一样,指定要使用的第一个元素和最后一个元素的索引,到达指定的第二个索引值前面的元素后停止。


players = ['charles','martina','michael','florence','eli']
print(players[0:3])
>>>['charles', 'martina', 'michael']

未指定起始索引及终止索引的情况: 


players = ['charles','martina','michael','florence','eli']
print(players[:4])
>>>['charles', 'martina', 'michael', 'florence']

players = ['charles','martina','michael','florence','eli']
print(players[1:])
>>>['martina', 'michael', 'florence', 'eli']

players = ['charles','martina','michael','florence','eli']
print(players[-3:])
>>>['michael', 'florence', 'eli']

6.遍历切片

要遍历列表的部分元素,可在for 循环中使用切片。


players = ['charles','martina','michael','florence','eli']
print("Here are the first three players in my team:")
for player in players[0:3]:
 print(player.title())
>>>Here are the first three players in my team:
>>>Charles
>>>Martina
>>>Michael

处理数据时,可使用切片来进行批量处理;编写Web应用程序时,可使用切片来分页显示信息。

7.复制列表

要复制列表,可创建一个包含整个列表的切片,方法是同时省略起始索引和终止索引([:] )。


my_foods = ['pizza','falafel','carrot cake']
friend_foods = my_foods[:]

print(my_foods)
print(friend_foods)
>>>['pizza', 'falafel', 'carrot cake']
>>>['pizza', 'falafel', 'carrot cake']


my_foods = ['pizza','falafel','carrot cake']
# friend_foods和my_foods指向同一个列表
friend_foods = my_foods
my_foods.append('cannoli')
friend_foods.append('ice cream')
print(my_foods)
print(friend_foods)
>>>['pizza', 'falafel', 'carrot cake', 'cannoli', 'ice cream']
>>>['pizza', 'falafel', 'carrot cake', 'cannoli', 'ice cream']

8.元组

列表是可以修改的,然而,需要创建一系列不可修改的元素,元组可以满足这种需求。不可变的列表被称为元组 。

元组看起来犹如列表,但使用圆括号而不是方括号来标识。


dimensions = (200,50)
print(dimensions[0])
print(dimensions[1])
>>>200
>>>50

元组元素不可更改: 


dimensions = (200,50)
dimensions[0] = 230

>>>dimensions[0] = 230
>>>TypeError: 'tuple' object does not support item assignment

8.1 for 循环遍历元组


dimensions = (200,50,100)
for dimension in dimensions:
 print(dimension)
>>>200
>>>50
>>>100

8.2修改元组变量

元组元素不可更改,但可给存储元组的变量赋值。


dimensions = (200,50,100)
for dimension in dimensions:
 print(dimension)

dimensions = (50,40,30)
for dimension in dimensions:
 print(dimension)
>>>200
>>>50
>>>100
>>>50
>>>40
>>>30

相比于列表,元组是更简单的数据结构。如果需要存储的一组值在程序的整个生命周期内都不变,可使用元组。

以上所述是小编给大家介绍的Python操作列表详解整合网站的支持!

来源:https://blog.csdn.net/weixin_43297213/article/details/88560127

标签:Python,操作列表
0
投稿

猜你喜欢

  • 让apache2以cgi方式运行perl cgi程序的实现方法

    2023-08-15 18:02:57
  • Python编程之string相关操作实例详解

    2023-01-05 04:17:47
  • python银行系统实现源码

    2022-08-09 16:05:20
  • 谈谈网页设计中的字体应用 (3) 实战应用篇·上

    2009-11-24 13:09:00
  • 详细讲解SQL Server数据库的文件恢复技术

    2009-01-15 12:54:00
  • Python中可以用三种方法判断文件是否存在

    2022-05-11 10:15:41
  • SQL SERVER2012中新增函数之字符串函数CONCAT详解

    2024-01-23 18:15:23
  • 使用python 写一个静态服务(实战)

    2023-09-29 15:57:25
  • SQL 2008 FileStream数据类型

    2008-10-28 21:07:00
  • pytorch + visdom 处理简单分类问题的示例

    2022-08-17 12:56:58
  • Mysql索引创建删除及使用代价

    2024-01-16 15:32:35
  • mysql 5.7更改数据库的数据存储位置的解决方法

    2024-01-21 11:56:43
  • 详谈Pandas中iloc和loc以及ix的区别

    2022-01-30 00:52:49
  • python PyQt5 爬虫实现代码

    2022-10-19 20:24:42
  • 用户体验的时间尺度[译]

    2009-10-30 18:25:00
  • Python 编码Basic Auth使用方法简单实例

    2023-06-13 22:29:50
  • 对Python3中bytes和HexStr之间的转换详解

    2022-12-10 22:15:36
  • python画图系列之个性化显示x轴区段文字的实例

    2023-01-16 06:15:24
  • 利用JavaScript正则表达式模拟Google Talk的文本处理

    2007-12-04 18:43:00
  • optgroup、sub、sup和bdo标签

    2009-07-26 18:39:00
  • asp之家 网络编程 m.aspxhome.com