练习 1:反转给定列表中的各元素
aLsit = [100, 200, 300, 400, 500]
预期输出:
[500、400、300、200、100]
![](http://image109.360doc.com/DownloadImg/2022/02/2111/239815170_1_20220221112313413_wm.png)
参考代码:
aList = [100, 200, 300, 400, 500]
aList = aList[::-1]
print(aList)
![](http://image109.360doc.com/DownloadImg/2022/02/2111/239815170_2_20220221112313758_wm.png)
练习 2:按索引连接两个列表
list1 = ["M", "na", "i", "Ke"]
list2 = ["y", "me", "s", "lly"]
预期输出:
['My', 'name', 'is', 'Kelly']
![](http://image109.360doc.com/DownloadImg/2022/02/2111/239815170_3_20220221112314335_wm.png)
参考代码:
list1 = ["M", "na", "i", "Ke"]
list2 = ["y", "me", "s", "lly"]
list3 = [i + j for i, j in zip(list1, list2)]
print(list3)
![](http://image109.360doc.com/DownloadImg/2022/02/2111/239815170_4_20220221112314663_wm.png)
练习 3:给定一个数字列表,将列表中的每一项的平方数
aList = [1, 2, 3, 4, 5, 6, 7]
预期输出:
[1, 4, 9, 16, 25, 36, 49]
![](http://image109.360doc.com/DownloadImg/2022/02/2111/239815170_5_20220221112315460_wm.png)
参考代码:
aList = [1, 2, 3, 4, 5, 6, 7]
aList = [x * x for x in aList]
print(aList)
![](http://image109.360doc.com/DownloadImg/2022/02/2111/239815170_6_20220221112315788_wm.png)
练习 4:依次按顺序连接两个列表
list1 = ["Hello ", "take "]
list2 = ["Dear", "Sir"]
预期输出:
['Hello Dear', 'Hello Sir', 'take Dear', 'take Sir']
![](http://image109.360doc.com/DownloadImg/2022/02/2111/239815170_7_20220221112316257_wm.png)
参考代码:
list1 = ["Hello ", "take "]
list2 = ["Dear", "Sir"]
resList = [x+y for x in list1 for y in list2]
print(resList)
![](http://image109.360doc.com/DownloadImg/2022/02/2111/239815170_8_20220221112316663_wm.png)
练习 5:同时迭代两个列表,以便 list1 应按原始顺序显示元素,而 list2 应按相反顺序显示元素
list1 = [10, 20, 30, 40]
list2 = [100, 200, 300, 400]
预期输出:
10 400
20 300
30 200
40 100
![](http://image109.360doc.com/DownloadImg/2022/02/2111/239815170_9_20220221112317288_wm.png)
参考代码:
list1 = [10, 20, 30, 40]
list2 = [100, 200, 300, 400]
for x, y in zip(list1, list2[::-1]):
print(x, y)
![](http://image109.360doc.com/DownloadImg/2022/02/2111/239815170_10_20220221112317741_wm.png)
今天的5个练习主要用到了Python中list列表的用法,分别使用切片返回列表的反转元素,列表推导式返回合并后的列表和列表元素的平方数等;大家都学会了吗?