Python 一行中13个相同字符的列表

Python 一行中13个相同字符的列表,python,list,recursion,input,character,Python,List,Recursion,Input,Character,到目前为止,对于我的代码,我有: while True: """ determines if there is a list of x's or o's in a horizontal row """ game = list(input()) if len(game) == 0: print("We should now check for vertical or diagonal winners!") elif game[0] =

到目前为止,对于我的代码,我有:

while True:
    """
    determines if there is a list of x's or o's in a horizontal row
    """
    game = list(input())
    if len(game) == 0:
        print("We should now check for vertical or diagonal winners!")
    elif game[0] == game[1]:
        if game[1] == game[2]:
            if game[2] == game[3]:
                if game[3] == game[4]:
                    if game[4] == game[5]:
                        if game[5] == game[6]:
                            if game[6] == game[7]:
                                if game[7] == game[8]:
                                    if game[8] == game[9]:
                                        if game[9] == game[10]:
                                            if game[10] == game[11]:
                                                if game[11] == game[12]:
                                                    if game[12] == "o":
                                                        print("Player o won")
                                                    else:
                                                        print("Player x won")
                                                else:
                                                    del game[0:12]
                                            else:
                                                del game[0:12]
                                        else:
                                            del game[0:12]
                                    else:
                                        del game[0:12]
                                else:
                                    del game[0:12]
                            else:
                                del game[0:12]
                        else:
                            del game[0:12]
                    else:
                        del game[0:12]
                else:
                    del game[0:12]
            else:
                del game[0:12]
        else:
            del game[0:12]
    else:
        del game[0:12]
我觉得应该有一个更短的方式来写这个。我也只想到了一个方法来确定是否有一个横向的赢家。我不知道如何去解决垂直或对角线的赢家。我还在第二行测试了这段代码,它没有打印x赢了,所以我想知道我的错误在哪里


非常感谢您的帮助!谢谢。

你急需了解regex,我的人

这看起来像是一个很可疑的家庭作业问题,但既然其他人都在回答……我会给你最快的答案:-)。对于这个问题,正则表达式可能比用纯python编写的任何东西都要快,因为它使用编译的c代码

您可以使用正则表达式直接从输入字符串测试水平或垂直匹配

import re

# find 13 x's or o's in a row that begin some multiple of 13 characters from the beginning of the input string
horizMatch_regex = re.compile("^(.{13})*(xxxxxxxxxxxxx|ooooooooooooo)")
# find 13 x's or o's that appear with exactly 12 characters in between, which corresponds to columns.  Requires lookahead (?=) 
verticalMatch_regex = re.compile("(x(.{12})(?=x)){12}|(o(.{12})(?=o)){12}")
# slightly trickier - you need 4 separate match groups to test for each possible diagonal.  There are a variety of ways to do that, but here's one
diagonalMatch_regex = re.compile("(^(x.{13}){12}x)|(^(o.{13}){12}o)|((x.{11}){13}.$)|((o.{11}){13}.$)")

if horizMatch_regex.search(input_str):
    print("We have a horizontal tic tac toe!")

if verticalMatch_regex.search(input_str):
    print("We have a vertical tic tac toe!")

if diagonalMatch_regex.search(input_str):
    print("We have a diagonal tic tac toe!")

# string with horizontal, vertical, and diagonal tic tac toe's
input_str = "xooooooooooooxxxxxxxxxxxxxxoxoooooooooxxxxxxxxxxxxxoxoooxoooooooxxxxxxxxxxxxxoxoooooxoooooxxxxxxxxxxxxxoxoooooooxoooxxxxxxxxxxxxxoxoooooooooxoxxxxxxxxxxxxxoxooooooooooox"

We have a horizontal tic tac toe!
We have a vertical tic tac toe!
We have a diagonal tic tac toe!

使用2D
列表和一些循环

instring = 'oooooooooooooxxxxxxxxxxxxoooooooooooooxxxxxxxxxxxxxoooooooooooooxxxxxxxxxxxxxoooooooooooooxxxxxxxxxxxxxoooooooooooooxxxxxxxxxxxxxoooooooooooooxxxxxxxxxxxxxooooooooooooox'

board = []
for x in range(13):
    board.append(instring[x::13])
board = list(zip(*board))
如果一行有赢家,请打印赢家:

>>> for row in range(13):
...     if len(set(board[row]))-2: print(board[row][0])
...
o
>>> for row in range(13):
...     if len(set(list(zip(*board))[row]))-2: print(board[row][0])
...
>>> if len(set(board[i][i] for i in range(13)))==1:
...     print(board[0][0])
...
>>> if len(set(board[i][i] for i in range(-1, -14, -1)))==1:
...     print(board[0][12])
...
如果列中有赢家,请打印赢家:

>>> for row in range(13):
...     if len(set(board[row]))-2: print(board[row][0])
...
o
>>> for row in range(13):
...     if len(set(list(zip(*board))[row]))-2: print(board[row][0])
...
>>> if len(set(board[i][i] for i in range(13)))==1:
...     print(board[0][0])
...
>>> if len(set(board[i][i] for i in range(-1, -14, -1)))==1:
...     print(board[0][12])
...
如果
\
形状的对角线有一个赢家,请打印赢家:

>>> for row in range(13):
...     if len(set(board[row]))-2: print(board[row][0])
...
o
>>> for row in range(13):
...     if len(set(list(zip(*board))[row]))-2: print(board[row][0])
...
>>> if len(set(board[i][i] for i in range(13)))==1:
...     print(board[0][0])
...
>>> if len(set(board[i][i] for i in range(-1, -14, -1)))==1:
...     print(board[0][12])
...
如果
/
形状的对角线有赢家,请打印赢家:

>>> for row in range(13):
...     if len(set(board[row]))-2: print(board[row][0])
...
o
>>> for row in range(13):
...     if len(set(list(zip(*board))[row]))-2: print(board[row][0])
...
>>> if len(set(board[i][i] for i in range(13)))==1:
...     print(board[0][0])
...
>>> if len(set(board[i][i] for i in range(-1, -14, -1)))==1:
...     print(board[0][12])
...

你学过循环吗?如果不是,这将是一个很好的时间。为什么这被标记为“递归”?@ScottHunter,因为从算法的角度来看,最简单的解决方案是递归的?我真的不理解你到目前为止的代码(你是绝对正确的,必须有一个更简单的方法来实现这一点,但因为我不太明白你想要做什么,所以我现在能告诉你的是del关键字对字符串不起作用。1)首先让它成为一个矩阵(列表列表);2)注意,检查列表
lst
中的所有字符是否都等于
ch
(让我们调用此函数
all_are(lst,char)
)与说明
lst[0]==ch和all_are(lst[1:],ch)
相同;所以你可以重新解决这个问题。好吧……好的,这是一个很好的观点。但我的想法是,如果我解释一下,解决对角线问题就太容易了。不过,我可能会继续发表完整的答案,b/c I是一个软件语句“Regex将比用纯python编写的任何东西都快,因为它使用编译的c代码”是错误的。某些正则表达式可能会遇到问题(例如灾难性的回溯),Python本身也大量使用C库(在正则表达式和其他领域)。显然正则表达式可能会非常慢,但我的意思是专门针对这个问题。在我的回答中阐明了这一点。如果这是解决这个问题的最快方法,那不是因为它访问C库。Python的许多部分都是这样做的。当然,它们是这样做的,但是针对这个问题的正则表达式允许您在Python intepreter中花费很少的时间-我只在3行Python代码中完成了这项工作,其余的都是用C完成的。这段代码对我来说不起作用。您可能正在使用哪一版本的python?在此上下文之外无法访问“行”:any(len(set(board[row]))-2表示范围(13)中的行)是奇数;它昨天对我有用,但现在不行。我会编辑它。