Python 我需要确保列表中只有某些字符?

Python 我需要确保列表中只有某些字符?,python,regex,string,list,python-2.7,Python,Regex,String,List,Python 2.7,我需要获取输入并将其放入列表中: 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

我需要获取输入并将其放入列表中:

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()
啊啊,但它无法工作。我肯定在那些代码块中做了一些错误的事情,我试图找出它,但它不起作用。了解我做错了什么会很好,但是解决我的问题会教会我更多我想要的东西。我甚至可能不需要使用re,但在我看来,这是一种节省空间的方式来实现我想要的。我认为我眼前的问题是,我不知道如何获得重用列表(除非(当然)训练有素的眼睛还能发现其他突出的问题)


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

我不懂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]

在如此小的范围内,您可以迭代move_顺序并检查允许的移动中是否存在每个元素

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()
编辑:考虑评论中建议的解决方案

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..

如果我理解你的问题,我不认为斯普利特在做你认为的事情。它不是将用户输入的字符串的每个字符拆分为一个数组

有很多方法可以匹配“ADSCH”
您可以使用
raw\u 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
all
move\u-order
列表一起使用(可与字符串一起使用):

any
move\u-order
列表一起使用(可与字符串一起使用):


之后你对这些角色做什么?可能根本不需要这个步骤。设计它,这样当你做动作的时候,你不会/不能做任何无效的动作

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()

您将列表发送到
re.match(模式、字符串、标志=0)
,是否区分大小写?可以接受移动顺序中的哪种项目,一个字符(即[a]、[S]、[D])?我想不区分大小写。用户最好能给出这样的输入:A、S、D、C、S、D、H等等(七个字符,唯一的规则是只能有一个H,一个S、C不能在H之后,我可能自己能弄清楚)哦,不是吗?哈哈哈,我当然会搞砸的:嗯,那我该怎么做呢(如果评论不是一个可以问更多问题的地方,那很抱歉)?我只想把他们写的每一封信都加到名单上。我会使用append还是别的什么?我在上面的代码中给出了一个例子,说明了一种方法。注释只能编辑五分钟。显然:P我很喜欢你的代码显示,并且摆脱了拆分,谢谢!这取决于您希望用户如何输入移动。在我的示例中,我使用原始代码的格式,让用户一次输入所有动作。如果您想使用append,您可以反复要求用户输入单个移动,直到组合完成?我建议
move\u order=list(原始输入(“输入您的移动”)
If set(move\u order)-set('ADSCH'):
。我认为应该修复
returnstart()
,即使没有特别提到它。
if any((x not in 'ADSCH' for x in move_order)):
    # it's an illegal input
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()