繁体   English   中英

如何通过 python 中的用户输入(也循环)将元素从列表 A 移动到列表 B?

[英]How do I move an element from list A to list B via the user's input (looped too) in python?

下面是我在 python 以及所需的 output 中运行的代码。 我把它放在一个循环中,将多个元素(名称)移动到第二个列表,直到用户输入一个空白。 唯一的问题是我无法围绕输入代码将一个名称从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

所需的 output:

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 相同,但使用 append。 你可以把它放在一个 if 语句中,它检查是否“chosenNames in regularLine”,如果你想删除多个名称,那么我会执行以下代码:

for item in chosenNames.split():
    if item in regularLine:
        regularLine.remove(item)
        fastTrack.append(item)
print("In the regular line we have", ' '.join(regularLine))
while 1:
  chosenName = input("Ready?\nEnter chosen names here:")
  if not chosenName:
    break
  regularLine.remove(chosenName)
  fastTrack.append(chosenName)
  print("You've now moved {} to the fast track.", chosenName)

使用index查找列表中项目的索引,然后将其pop ,然后append到第 2 个列表。

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."
)
print(
    "In the regular line we have {}, {}, {}, {}, {}, \n{}, {}, {}, {}, {}, {}, and {}."
    .format(*regularLine))
print("Ready?")

while True:
    chosenName = input("Enter chosen names here: ")
    if not chosenName:
        break
    try:
        i = regularLine.index(chosenName)
    except ValueError:
        print(f"{chosenName} is not here!")
        continue
    fastTrack.append(regularLine.pop(i))

print("You've now moved {} to the fast track.".format(", ".join(fastTrack) or "no one"))

不要将输入传递给 remove 和 append 函数,而是变量本身。 此外, .join(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(str("Ready?\nEnter chosen names here:"))
    if chosenNames in regularLine:
        fastTrack.append(chosenNames)
        regularLine.remove(chosenNames)
    print("You've now moved {} to the fast track.".format(", ".join(fastTrack)))
    if chosenNames == "":
        NotBlank = False
        break
    else:
        NotBlank = True

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM