程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

python入门——操作列表

发布于2019-08-22 17:42     阅读(994)     评论(0)     点赞(23)     收藏(3)


遍历整个列表
使用for 循环来打印魔术师名单中的所有名字:

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

输出很简单,就是列表中所有的姓名:

alice
david
carolina
  • 1
  • 2
  • 3

在for 循环中,可对每个元素执行任何操作。下面来扩展前面的示例,

magicians = ['alice', 'david', 'carolina']
for magician in magicians:
    print(magician.title() + ", that was a great trick!")
  • 1
  • 2
  • 3

下面的输出表明,对于列表中的每位魔术师,都打印了一条个性化消息:

Alice, that was a great trick!
David, that was a great trick!
Carolina, that was a great trick!
  • 1
  • 2
  • 3

在for 循环后面,没有缩进的代码都只执行一次,而不会重复执行。

magicians = ['alice', 'david', 'carolina']
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!")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

你在前面看到了,开头两条print 语句针对列表中每位魔术师重复执行。然而,由于第三条print 语句没有缩进,因此只执行一次:

Alice, that was a great trick!
I can't wait to see your next trick, Alice.

David, that was a great trick!
I can't wait to see your next trick, David.

Carolina, that was a great trick!
I can't wait to see your next trick, Carolina.

Thank you, everyone. That was a great magic show!
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

创建数值列表
Python函数range() 让你能够轻松地生成一系列的数字。

for value in range(1,5):
    print(value)
  • 1
  • 2

上述代码好像应该打印数字1~5,但实际上它不会打印数字5:

1
2
3
4
  • 1
  • 2
  • 3
  • 4

要创建数字列表,可使用函数list() 将range() 的结果直接转换为列表。如果将range() 作为list() 的参数,输出将为一个数字列表。

numbers = list(range(1,6))
print(numbers)
  • 1
  • 2

结果如下:

[1, 2, 3, 4, 5]
  • 1

使用函数range() 时,还可指定步长。例如,下面的代码打印1~10内的偶数:

even_numbers = list(range(2,11,2))
print(even_numbers)
  • 1
  • 2

在这个示例中,函数range() 从2开始数,然后不断地加2,直到达到或超过终值(11),因此输出如下:

[2, 4, 6, 8, 10]
  • 1

在Python中,两个星号(** )表示乘方运算。下面的代码演示了如何将前10个整数的平方加入到一个列表中:

squares = []
for value in range(1,11):
    square = value**2
    squares.append(square)

    print(squares)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

首先,我们创建了一个空列表;接下来,使用函数range() 让Python遍历1~10的值。在循环中,计算当前值的平方,并将结果存储到变量square 中。然后,将新计算得到的平方值附加到列表squares 末尾。最后,循环结束后,打印列表squares :

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
  • 1

下面的示例使用列表解析创建你在前面看到的平方数列表:

squares = [value**2 for value in range(1,11)]
print(squares)
  • 1
  • 2

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

结果与你在前面看到的平方数列表相同:

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
  • 1

使用列表的一部分

要创建切片,可指定要使用的第一个元素和最后一个元素的索引。与函数range() 一样,Python在到达你指定的第二个索引前面的元素后停止。要输出列表中的前三个元素,需要指定索引0~3,这将输出分别为0 、1 和2 的元素。

下面的示例处理的是一个运动队成员列表:

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

其中只包含三名队员。输出也是一个列表,其中包含前三名队员:

['charles', 'martina', 'michael']
  • 1

如果你没有指定第一个索引,Python将自动从列表开头开始:

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

由于没有指定起始索引,Python从列表开头开始提取:

['charles', 'martina', 'michael', 'florence']
  • 1

要让切片终止于列表末尾,也可使用类似的语法。例如,如果要提取从第3个元素到列表末尾的所有元素,可将起始索引指定为2 ,并省略终止索引:

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

Python将返回从第3个元素到列表末尾的所有元素:

['michael', 'florence', 'eli']
  • 1

如果你要输出名单上的最后三名队员,可使用切片players[-3:] :

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

上述代码打印最后三名队员的名字,即便队员名单的长度发生变化,也依然如此。

如果要遍历列表的部分元素,可在for 循环中使用切片。在下面的示例中,我们遍历前三名队员,并打印他们的名字:

players = ['charles', 'martina', 'michael', 'florence', 'eli']

print("Here are the first three players on my team:")
for player in players[:3]:
    print(player.title())
  • 1
  • 2
  • 3
  • 4
  • 5

代码没有遍历整个队员列表,而只遍历前三名队员:

Here are the first three players on my team:
Charles
Martina
Michael
  • 1
  • 2
  • 3
  • 4

要复制列表,可创建一个包含整个列表的切片,方法是同时省略起始索引和终止索引([:] )。这让Python创建一个始于第一个元素,终止于最后一个元素的切片,即复制整个列表。

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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

打印每个列表后,我们发现它们包含的食品相同:

My favorite foods are:
['pizza', 'falafel', 'carrot cake']

My friend's favorite foods are:
['pizza', 'falafel', 'carrot cake']
  • 1
  • 2
  • 3
  • 4
  • 5

定义元组

元组看起来犹如列表,但使用圆括号而不是方括号来标识。定义元组后,就可以使用索引来访问其元素,就像访问列表元素一样。

dimensions = (200, 50)
print(dimensions[0])
print(dimensions[1])
  • 1
  • 2
  • 3

接下来,我们分别打印该元组的各个元素,使用的语法与访问列表元素时使用的语法相同:

200
50
  • 1
  • 2

像列表一样,也可以使用for 循环来遍历元组中的所有值:

dimensions = (200, 50)
for dimension in dimensions:
    print(dimension)
  • 1
  • 2
  • 3

就像遍历列表时一样,Python返回元组中所有的元素:

200
50
  • 1
  • 2


所属网站分类: 技术文章 > 博客

作者:齐天大圣

链接:https://www.pythonheidong.com/blog/article/53246/bf8fbddc89c6e15450dc/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

23 0
收藏该文
已收藏

评论内容:(最多支持255个字符)