0

我有一个数据文件,其中包括未知棋盘游戏的“快照”,例如 tictactoe/dama/chess/go..etc。但是我不知道游戏的参数,例如棋盘的尺寸,棋子的类型..等

最简单的情况是tictactoe,所以让我们以它为例。块和空字段表示为数字(-n,-n+1.. 0, +n-1..+n.. )

开始:

  • 0 0 0
  • 0 0 0
  • 0 0 0

在这个简单的例子中,每次移动(x,O 由 1 或 -1 表示,空字段为 0。)。最后,我将有一组由两条空行分隔的 3x3 矩阵。

我如何将数据读入 ndim 数组([length_of_game][board_width][board_length]而无需手动添加有关游戏bor/length 大小的任何信息

我只知道我有一块未知大小的棋盘,不同的棋子用不同的数字表示,快照代表了游戏的演变。

4

1 回答 1

1

一种方法是逐行解析文件。用空格分割行(假设一行中的数字由空格分隔)并将结果列表添加到另一个列表中(让我们称之为 current_game),它将保存所有行(行数据)。当您遇到空行时,您可以将 current_game 列表添加到另一个列表中(我们称之为一个游戏),它将保存所有游戏。

这是一个示例函数,它将执行此操作:

def parse_data_file(file_path):
    games = []
    current_game = []
    with open(file_path, mode='r',) as file_reader:
        for line in file_reader:
            if len(line.strip()) == 0:
                if len(current_game) > 0:
                    # A empty new line, so the current game has finished. Add the current game to the games.
                    games.append(current_game)
                    current_game = []
            else:
                current_game.append(line.strip().split())

    return games

该函数正在检查当前行长度是否大于 0,如果是,则首先对其进行条带化(从行尾删除任何空白),然后将其拆分为空白。您可以在此处阅读有关拆分功能的更多信息。如果 line length 等于 0,并且 current_game 长度大于 0(此检查是在游戏列表中只添加一次 current_game),它将将该列表添加到游戏列表中,并将其设置为新的空列表。

如果要将列表中的字符串转换为整数,可以在拆分行时使用map函数。这是将字符串转换为整数的相同代码:

def parse_data_file(file_path):
    games = []
    current_game = []
    with open(file_path, mode='r',) as file_reader:
        for line in file_reader:
            if len(line.strip()) == 0:
                if len(current_game) > 0:
                    # A empty new line, so the current game has finished. Add the current game to the games.
                    games.append(current_game)
                    current_game = []
            else:
                current_game.append(map(lambda item: int(item), line.strip().split()))

    return games

最后,要将列表转换为 numpy ndim 数组,您可以使用 numpy 中的数组函数。这个解决方案假设在最后一场比赛之后,会有两条空行,但很容易改变它。

于 2017-09-28T14:40:05.043 回答