Python学习——如何操作列表

利用for循环来遍历列表

循环是让计算机自动完成重复工作的常见方式之一

magicians = ['alice','david','carolina']
>>> for magician in magicians: #依次循环获取列表maicians中的值 并存储到变量magician中
	print(magician)

	
alice
david
carolina

在循环中还可以对每个元素进行执行操作

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

Carolina,that was a great trick!
Carolina,that was a great trick!
Carolina,that was a great trick!

在for循环中可以包含多条代码,例如在 for magicain in magicians后面,每个缩进的代码行都是循环的一部分,对列表中的每个值都执行一次。

注意:避免缩进错误,python根据缩进来判断代码行前一行代码行的关系,通过缩进能让代码更易读,简单说,需要缩进来使代码整洁而结构清晰。其次不要忘记for语句后面要加冒号来告诉python,下一行是循环的第一行。

创建数字列表

使用range()函数

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

	
1
2
3
4

在此例中,range()只是打印数字1-4,这是在编程语言中经常看到的差一行为的结果。

使用range()创建数字列表

在前例中,我们打印了一系列数字,现在利用list()函数将这些数字转换为一个列表。

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

使用range()函数还可以指定步长,例如打印1-10内的偶数:

>>> even_numbers = list(range(2,11,2))#函数从2开始数,不断加2,直到达到或超过终值(11)
>>> print(even_numbers)
[2, 4, 6, 8, 10]

在创建一个包括前十个整数的平方的列表:

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

列表解析可以简化代码,将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循环,用于给表达式提供值,再加上右括号。

使用列表的一部分

切片

处理部分元素时

>>> player = ['charles','martina','michael','florence','eli']
>>> print(player[0:3])
['charles', 'martina', 'michael']
>>> print(player[:4])#从第一个数字开始,第三个终止
['charles', 'martina', 'michael', 'florence']
>>> print(player[1:])#从第二个数字开始,最后一个结束
['martina', 'michael', 'florence', 'eli']
>>> print(player[-3:])#倒数第三个开始,最后一个结束
['michael', 'florence', 'eli']

遍历切片:

players = ['charles','martina','michael','florence','eil']
print("Here are the first three players on my team:")
for player in players[:3]:
    print(player.title())

Here are the first three players on my team:
Charles
Martina
Michael

复制列表:

my_foods = ['pizza','falafel','carrot cake']
friend_foods = my_foods[:]
print("my favorite foods are:")
print(my_foods)
print("\nMy friend's favorite food are:")
print(friend_foods)

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

My friend's favorite food are:
['pizza', 'falafel', 'carrot cake']

在各列表中添加元素,注意在复制时不能丢掉[:],否则就不能得到两个相同的列表:

my_foods = ['pizza','falafel','carrot cake']
friend_foods = my_foods[:]
my_foods.append("cannoli")
friend_foods.append('ice cream')
print("my favorite foods are:")
print(my_foods)
print("\nMy friend's favorite food are:")
print(friend_foods)

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

My friend's favorite food are:
['pizza', 'falafel', 'carrot cake', 'ice cream']

关于元组

元组看起来犹如列表,但使用圆括号而不是方括号来标识,并且元组中的值是不可修改,也就是说不可变的列表叫做元组

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

遍历元组:

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

虽然不能修改元组中的元素,但可以给存储元组的变量赋值:

dimensions = (200,50)
print("original dimensions:")
for dimension in dimensions:
	print(dimension)
dimension = (400,100)
print("\nModified dimension:")
for dimension in dimensions:
    print(dimension)


original dimensions:
200
50

Modified dimension:
200
50

后记:
前期因为练车,最近因为过年疫情,已经懒惰了一段时间,没有做自己热爱的事情,学习自己感兴趣的东西,没有好好把敲代码继续的进行下去,也开始为工作焦虑,甚至陷入其中,开始give up,这个过程中逐渐开始失去人生的方向,忘记理想长什么样子了,但所有的焦虑都来自无聊的时光,自律的人永远在前进,即使今天遇到麻烦,他也会思考,去控制自己的行为,改变现状,继续前进。
切记不要懒惰,不要轻易放弃!脑袋和身体都要动起来,自律即强大,目标即远方。不要让别人告诉你,你做这个不行做那个不行,make your own decision,and protect your dream!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值