【问题标题】:Python - load unknown data as n-dim matrixPython - 将未知数据加载为 n-dim 矩阵
【发布时间】:2017-09-28 11:30:00
【问题描述】:

我有一个数据文件,其中包含 未知 棋盘游戏的“快照”,例如 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 大小的信息 游戏?

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

【问题讨论】:

  • 不遗憾。它与 mathlab 无关,它与未知的 ndim 数组有关,与固定长度无关。 (更一般)
  • 从来没有说过这是关于matlab的。当我在你的问题中读到你的字句时,我假设你想知道如何创建一个井字游戏字段数组。如果链接不相关,则不相关:)
  • 你说得对,我改进了我的问题以便更好地理解。

标签: python image machine-learning game-engine read-data


【解决方案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,如果是,则首先对其进行条带化(从行尾删除任何空白),然后将其拆分为空白。您可以阅读更多关于拆分功能here 的信息。如果 line 长度等于 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 中的 array 函数。 这个解决方案假设在最后一场比赛之后,会有两个空行,但很容易改变。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多