专注于 JetBrains IDEA 全家桶,永久激活,教程
持续更新 PyCharm,IDEA,WebStorm,PhpStorm,DataGrip,RubyMine,CLion,AppCode 永久激活教程

列表的拷贝

将一个列表的值复制到另一个列表,如将my_foods拷贝到friend_foods,需要使用如下的程序,才可分别对这两个列表的值进行修改,两个列表的值可以不同

my_foods = [’pizza’, ‘falafel’, ‘carrot cake’]

friend_foods = my_foods[:] #也可使用 friend_foods = my_foods.copy()

my_foods.append(‘cannoli’)

friend_foods.append(‘ice cream’)

print(“My favorite foods are:”)

print(my_foods)

print(“\nMy friend’s favorite foods are:”)

print(friend_foods)

输出

My favorite foods are:

[’pizza’, ‘falafel’, ‘carrot cake’, ‘cannoli’]

My friend’s favorite foods are:

[’pizza’, ‘falafel’, ‘carrot cake’, ‘ice cream’]

如果使用

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 foods are:”)

print(friend_foods)

输出

My favorite foods are:

[’pizza’, ‘falafel’, ‘carrot cake’, ‘cannoli’, ‘ice cream’]

My friend’s favorite foods are:

[’pizza’, ‘falafel’, ‘carrot cake’, ‘cannoli’, ‘ice cream’]

两个列表的值始终相同

参考4、4.3节

文章永久链接:https://tech.souyunku.com/21712

未经允许不得转载:搜云库技术团队 » 列表的拷贝

JetBrains 全家桶,激活、破解、教程

提供 JetBrains 全家桶激活码、注册码、破解补丁下载及详细激活教程,支持 IntelliJ IDEA、PyCharm、WebStorm 等工具的永久激活。无论是破解教程,还是最新激活码,均可免费获得,帮助开发者解决常见激活问题,确保轻松破解并快速使用 JetBrains 软件。获取免费的破解补丁和激活码,快速解决激活难题,全面覆盖 2024/2025 版本!

联系我们联系我们