根据用户输入替换列表中的项

6 投票
2 回答
8523 浏览
提问于 2025-04-18 09:01

我还是个新手,请多多包涵。

这里有三个列表:

  1. 一个字母列表 L = ['A','B','C','D','E']
  2. 一个数字列表 N = ['1','2','3','4','5']
  3. 一个数字字符串列表 List = ['124','351']

我想要完成的步骤如下:

  1. 向用户请求一个字母,比如 A。
  2. 在字母列表 L 中找到这个字母,并记录它的位置,比如 [0]。
  3. 用同样的位置去查数字列表 N,记录下那里的数字,比如 1。
  4. 把在数字字符串 List 中找到的数字替换成刚才的字母,比如 ['124','351'] 变成 ['A24','35A']。
  5. 继续询问用户下一个字母,直到所有的数字字符串都变成字母。

到目前为止,我已经完成了前四个步骤。在第四步之后,我想检查数字字符串中是否还包含数字,如果有,就继续进行第五步。但是我不知道怎么写代码来检查数字字符串中是否还有数字。注意:数字列表不只限于数字,它也可能包含数学符号,比如 + 或 -。

L = ['A','B','C','D','E']
N = ['1','2','3','4','5']
list = ['124','351']

print ("Enter a letter")

# Is there a number in List
# If yes then do the following else print List

# Ask for a letter from the user
letter = input ("Enter letter: ")

# Confirm whether the letter is correct or not
if letter in L:
# Find the position of the letter in the list
    position = (L.index(letter));
# Make a variable called number with value at the same position in the N list
    number = N[position];
# Replace the numbers in the List with the letter entered
    list = [item.replace(number, letter) for item in list];
# Print the list with the numbers replaced
    print (list, "\n");
    print ("Please guess again. \n");
    letter = input ("Enter a letter now: ")
# repeat until the List only contains letters
else:
    print ("That is not correct");
    print ("Please guess again. \n");
    letter = input ("Enter a letter now: ")

希望这样没问题。如果你需要更多信息,请告诉我。

2 个回答

1

我似乎搞不清楚怎么让代码检查数字字符串里是否还有其他数字。

你可以定义一个函数,循环遍历这个列表,看看里面有没有包含数字的条目,可以用 isdigit() 方法,像这样:

def has_number(lst):
    for s in lst:
        if any(x.isdigit() for x in s):
            return True
    return False

如果你的数字字符串列表中有任何条目包含数字,这个方法会返回 True。


我看到了你的编辑。

我的目标是让它只包含字母。

要做到这一点,你可以这样检查:

if all(x.isalpha() for x in lst):
    # lst contains only entries that consists of letters

这个方法使用了 isalpha()

str.isalpha()

如果字符串中的所有字符都是字母,并且至少有一个字符,就返回 True;否则返回 False。

示例:

>>> all(x.isalpha() for x in ['abc', 'def'])
True
>>> all(x.isalpha() for x in ['ab1', 'def'])
False
1
L = ['A','B','C','D','E']
N = ['1','2','3','4','5']
n_strings = ['124','351'] # Don't use list as a variable name

while not all( x.isalpha() for x in n_strings): # keep going until all are alpha chars

    print ("Enter a letter")
    # Is there a number in List
    # If yes then do the following else print List

    # Ask for a letter from the user
    letter = input("Enter letter: ")

    # Confirm whether the letter is correct or not
    if letter in L:
    # Find the position of the letter in the list
        position = (L.index(letter));
    # Make a variable called number with value at the same position in the N list
        number = N[position];
    # Replace the numbers in the List with the letter entered
        n_strings = [item.replace(number, letter) for item in n_strings];
    # Print the list with the numbers replaced
        print (n_strings, "\n");
        print ("Please guess again. \n");
        letter = input("Enter a letter now: ")
    # repeat until the List only contains letters
    else:
        print ("That is not correct");
        print ("Please guess again. \n");
        letter = input("Enter a letter now: ")

你可以调整一下逻辑,让代码变得更简洁。

while True:
    if all(x.isalpha() for x in n_strings ):
        print("All guessed correct {}".format(n_strings)) # if all are alpha print final n_string and break out of loop
        break
    print n_strings    
    letter = input("Please enter a letter: ")
    if letter in L:
    # Find the position of the letter in the list
        position = (L.index(letter));
        number = N[position];
        n_strings = [item.replace(number, letter) for item in n_strings];
        print (n_strings, "\n");
    # repeat until the List only contains letters
    else:
        print ("That is not correct");
        print ("Please guess again. \n");

撰写回答