扩展列表
既然你可以修改列表中的元素,那么自然也会希望能在列表中添加元素,对吧?使用 + 运算符就能实现:
x = ["a", "b", "c", "d"]
y = x + ["e", "f"]
你中了彩票,太棒了!你决定建一个泳池小屋 (poolhouse) 和一个车库 (garage)。你能把这些信息添加到 areas 列表中吗?
本练习是课程的一部分
Python 入门
练习说明
- 使用
+运算符将列表["poolhouse", 24.5]拼接到areas列表末尾。将结果列表存储到areas_1中。 - 继续扩展
areas_1,添加关于车库的数据。添加字符串"garage"和浮点数15.45。将结果列表命名为areas_2。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Create the areas list and make some changes
areas = ["hallway", 11.25, "kitchen", 18.0, "chill zone", 20.0,
"bedroom", 10.75, "bathroom", 10.50]
# Add poolhouse data to areas, new list is areas_1
areas_1 = ____
# Add garage data to areas_1, new list is areas_2
areas_2 = ____