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

时间:2014-06-08 13:40:00

标签: python string

我是个菜鸟,所以请原谅。

有三个列表

  1. 字母列表L = ['A','B','C','D','E']
  2. 数字列表N = ['1','2','3','4','5']
  3. 数字字符串列表List = ['124','351']
  4. 这些是我希望实现的步骤

    1. 索取用户的来信,例如A
    2. 在字母列表 L 中找到该字母并记录其数字位置,例如[0]
    3. 在数字列表中使用相同的数字位置 N 并记录 那里的数字,例如1
    4. 替换非字母字符串列表中找到的号码实例,例如['124','351']变为['A24','35A']
    5. 询问用户下一个字母,直到所有数字字符串都成为字母。
    6. 到目前为止,我所取得的成就是前4个步骤。在第4步之后,我想检查数字字符串是否仍然包含数字,如果是,请转到第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 个答案:

答案 0 :(得分: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   字母,至少有一个字符,否则为假。

演示:

>>> all(x.isalpha() for x in ['abc', 'def'])
True
>>> all(x.isalpha() for x in ['ab1', 'def'])
False

答案 1 :(得分: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");