python - 如何通过python中的用户输入(也循环)将元素从列表A移动到列表B?
问题描述
下面是我在 python 中运行的代码,以及所需的输出。我把它放在一个循环中,将多个元素(名称)移动到第二个列表,直到用户输入一个空白。唯一的问题是我无法围绕输入代码将一个名称从regularLine 移动到FastTrack。
代码:
regularLine = ["Ryan","Luke","Chase","Scotty","Brayden","Ben","James","Daniel","Carson","Nathalia","Ian","Dave"]
fastTrack = []
print("Hello, my name is Rebo and today you'll be choosing who gets to \ngo into the fast track.\n")
print("In the regular line we have {}, {}, {}, {}, {}, \n{}, {}, {}, {}, {}, {}, and {}.\n".format(*regularLine))
NotBlank = True
while NotBlank is True:
chosenNames = input("Ready?\nEnter chosen names here:")
regularLine.remove(input)
fastTrack.append(input)
print("You've now moved {} to the fast track."fastTrack)
if chosenNames == "":
NotBlank is False
break;
else:
NotBlank is True
期望的输出:
print("You've now moved {} to the fast track.".format(*fastTrack))
如果还没看过,
regularLine.remove(input)
fastTrack.append(input)
是我想要输入的两行,用于从列表 A 中删除名称并将其添加到列表 B。如果我将列表转换为文件可能会更容易,但我会喜欢保持原样。
解决方案
不要做 regularLine.remove(input) 做 regularLine.remove(chosenNames) 和 fastTrack 一样,但要追加。你可以把它放在一个 if 语句中,它检查是否“chosenNames in regularLine”,如果你想删除多个名称,那么我会执行以下代码:
for item in chosenNames.split():
if item in regularLine:
regularLine.remove(item)
fastTrack.append(item)
推荐阅读
- javascript - 仅为特定小部件设置 RTL 方向
- python-3.x - IndexError:元组索引超出范围(Frappe/Erpnext)
- c++ - 如何在不同的行中多次获得cin?
- firebase - Firestore 分布式计数器:如何知道我们需要的理想分片总数?
- unity3d - 团结 | mesh.colors 不会为我的自定义网格对象着色
- excel - 是否有查找“结尾”的功能
- keras - 从 keras 中另一个模型的输出初始化模型的权重以进行迁移学习
- delphi - 如何在 Case 命令中创建多个条件?
- python - Pandas & Plotly:如何访问悬停文本中不用于绘制点的数据列?
- sql - 检索第二个日期值