【问题标题】:I need to make sure that only certain characters are in a list?我需要确保列表中只有某些字符?
【发布时间】:2015-09-08 16:18:31
【问题描述】:

我有这个来获取输入并将其放入列表中:

def start():
    move_order=[raw_input("Enter your moves: ").split()]

我只希望允许使用字符 A、D、S、C、H(这是为了游戏>_>)。我试过使用正则表达式的东西:

if re.match('[ADSCH]+', [move_order]) is False:
    print "That's not a proper move!"
    return start()

...以不同的形式...

string_test=re.compile('[ADSCH]+')
    if string_test.match(move_order]) is False:
        print "That's not a proper move!"
        return start()

Aaaa 无法让它工作。我肯定在这些代码块中做错了什么,我试图弄清楚,但它不起作用。了解我做错了什么会很好,但我的问题的解决方案会让我学到更多我想要的东西。我什至可能不需要使用 re,但在我看来,这是一种实现我想要的空间有效的方式。我认为我的直接问题是我不知道如何重新使用列表(除非(当然)训练有素的眼睛可以发现其他明显的问题)。

我会继续问,因为我可能也会把这搞砸,但我还需要确保 C 永远不会在 H 之后......但一个小小的提示是可以接受的,因为我喜欢弄清楚事情。

【问题讨论】:

  • 您将列表发送到re.match(pattern, string, flags=0),是否区分大小写? move_order 中什么样的项目可以接受,单个字符(即 ['A', 'S', 'D'])?
  • 我想不区分大小写。理想情况下,用户会给出如下输入:AASDCHD 或 DCASADH 等(七个单个字符,唯一的规则是只能有一个 H 和一个 S 和 C 不能在 H 之后,我可能可以自己弄清楚)

标签: python regex string list python-2.7


【解决方案1】:

有很多方法可以匹配“ADSCH”
您可以使用raw_input().upper() 摆脱'adsch'

使用re:之前不要拆分

def start():
    movement = raw_input("Enter your moves: ").upper()
    if re.match('^[ADSCH\s]*$', movement):
        # it's a legal input

使用str.strip

if movement.strip(' ADSCH') == '':
    # it's a legal input

使用allmove_order 列表(也可以使用字符串):

def start():
    move_order=[raw_input("Enter your moves: ").upper().split()]
    if all((x in 'ADSCH' for x in move_order)):
        # it's a legal input

使用anymove_order 列表(也可以使用字符串):

if any((x not in 'ADSCH' for x in move_order)):
    # it's an illegal input

【讨论】:

    【解决方案2】:

    有了这么小的范围,你可以迭代 move_order 并检查每个元素是否存在于允许的移动中

    def start():
        move_order=[c for c in raw_input("Enter your moves: ")]
        moves = ['A','D','S','C','H']
        for c in move_order:
            if c not in moves:
                print "That's not a proper move!"
                return start()
    

    编辑:考虑到 cmets 建议的解决方案

    def start():
    move_order=list(input("Enter your moves: "))
        while set(move_order) - set('ADSCH'):
            for x in set(move_order) - set('ADSCH'):
              move_order = [input("%s is not a vaild move, please enter another move" % x) if i==x else i for i in move_order]
        Print "Player ready" #Rest of program..
    

    如果我理解你的问题,我不认为 split 正在做你认为的那样。它不是将用户输入的字符串的每个字符拆分为一个数组。

    【讨论】:

    • 哦,不是吗?哈哈哈当然我会搞砸了:P uhhh 那我该怎么做(对不起,如果 cmets 不是问更多问题的地方)?我只想将他们放入的每个字母都添加到列表中。我会使用 append 还是什么?
    • 我在上面的代码中给出了一种方法的例子
    • 哦 cmets 显然只能编辑五分钟:P 我确实喜欢您的代码显示并摆脱了拆分,谢谢!
    • 这取决于您希望用户如何输入动作。在我的示例中,我使用原始代码的格式并让用户一次输入所有动作。如果你想使用追加,你可以反复要求用户输入一个移动,直到组合完成?
    • 我推荐move_order = list(raw_input("Enter your moves: "))if set(move_order) - set('ADSCH'):。而且我认为return start() 应该被修复,即使它没有被特别提及。
    【解决方案3】:

    我不知道 python 但你可以这样做:

    for c in move_order:
    if (c == 'A' or c == 'D' c == 'S' or c == 'C' or c == 'H'):
    [do something with the character]
    

    【讨论】:

      【解决方案4】:

      之后你对角色做了什么?甚至可能不需要这一步。设计它,以便当您为移动执行操作时,您不会/不能执行任何无效的操作。

      def move_left():
          print "Moving left"
      
      def move_down():
          print "moving down"
      
      #...etc
      
      def invalid_move():
          print "invalid move"
      
      # This dictionary connects move command letters
      # with the functions above that do the moving
      move_funcs = {
          'A': move_left,
          'S': move_down,
          'D': move_right,
          'C': wtf_keyboard_layout,
          'H': do_H_thing
      }
      
      moves = raw_input("Enter your moves: ")
      for move in moves.upper():
      
          # this gets the right function for the move, 
          # e.g. A gets move_left
          # but any character not there gets invalid_move
          move_func = move_funcs.get(move, invalid_move)
          move_func()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-03-23
        • 2023-03-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-07
        • 2010-12-09
        相关资源
        最近更新 更多