python - Python初学者 - 如何从列表中召回已删除的项目
问题描述
我是一个 python 初学者,我想知道一旦列表中的项目被替换,它可以被召回吗?
Friends=["Jen","Lam","Sam","Song"]
print (Friends)
#replace a list item
Friends[0] = "Ken"
print (Friends)
如果我想说 Jen 在 python 中被 Ken 取代,而不是直接写出print("Jen")
并使用变量,我应该怎么写。
解决方案
Friends[0]
在更换它之前跟踪它。
IE
friends=["Jen","Lam","Sam","Song"]
print(Friends)
replaced = friends[0]
friends[0] = "Ken"
print(replaced + " was replaced by " + friends[0])
您也可以使用pop
和insert
。
friends=["Jen","Lam","Sam","Song"]
print(friends)
replaced = friends.pop(0)
friends.insert(0, "Ken")
print(replaced + " was replaced by " + friends[0])
推荐阅读
- build - Xcode 10.2 在任意任务存档期间挂起
- javascript - Tinymce5手机回复背景色问题
- javascript - 显示 image1 的脚本,之后(随机 ms)显示 image2 并停止
- c# - 如何避免在数据库、ERP 上创建列
- angular - 如何在 HTML 中迭代 FormArray?
- reactjs - 如何在 ReactJS 中导航到同一页面上的特定 div?
- .net-core - Bot 在 Microsoft Teams 中的行为不同
- firebase - 如何在 Firebase 上为每个实时数据库授予不同的 IAM 权限?
- imagemagick - 使用 imagemagick 根据渐变设置 alpha
- javascript - JavaScript 链接 VS CSS 链接