简体   繁体   English

Python-将未知数据加载为n维矩阵

[英]Python - load unknown data as n-dim matrix

I have a data file including the "snapshots" of an unknown boardgame, such as tictactoe/dama/chess/go..etc. 我有一个数据文件,其中包括一个未知棋盘游戏的“快照”,例如tictactoe / dama / chess / go..etc。 But i don't know the parameters of the game, such as dimension of the board, type of pieces..etc 但是我不知道游戏的参数,例如棋盘的尺寸,棋子的类型等等。

The easiest case is the tictactoe so lets take it as an example. 最简单的情况是抽动,所以以它为例。 Pieces and empty fields are represented as numbers (-n,-n+1.. 0, +n-1..+n.. ) 件和空白字段用数字(-n,-n + 1 .. 0,+ n-1 .. + n ..)表示

Start: 开始:

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

In this simple case each move (x,O is represented by 1 or -1, empty field is 0. ). 在这种简单情况下,每次移动(x,O用1或-1表示,空字段为0。)。 At the end i'll have a set of 3x3 matrices separated by two empty lines. 最后,我将得到一组由两个空行分隔的3x3矩阵。

How can i read the data into an ndim array ([length_of_game][board_width][board_length] without manually adding any information about the size of the bor/length of the game? 我如何将数据读入ndim数组 ([length_of_game] [board_width] [board_length], 而无需手动添加有关游戏的口径/长度的信息)

I know only that i have a board with unknown size, different pieces are represented by different numbers, and the snapshots represents the evolution of the game. 我只知道我的棋盘尺寸未知,不同的棋子用不同的数字表示,而快照则表示游戏的发展。

One way that you can do this is to parse the file line by line. 您可以执行此操作的一种方法是逐行分析文件。 Split the line by white space (assuming that the numbers in one line are separated by white space) and add the resulting list in an other list (lets call this current_game) which will hold all of the rows (lines data). 将行用空格分隔(假设一行中的数字由空格分隔),然后将结果列表添加到将容纳所有行(行数据)的另一列表中(称为此current_game)。 When you encounter an empty line, you can add the current_game list in another list (lets call this one games), which will hold all the games. 当您遇到空白行时,可以将current_game列表添加到另一个列表中(让我们将其称为一个游戏),该列表将容纳所有游戏。

Here is an example function which will do that: 这是一个示例函数,它将执行此操作:

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

The function is checking if the current line length is bigger than 0, and if it is, then it is first striping it (removing any white space from the end of the line), and then splitting it by white space. 该函数正在检查当前行的长度是否大于0,如果大于0,则首先将其剥离(从行尾删除任何空白),然后按空白将其分割。 You can read more about the split function here . 您可以在此处阅读有关split函数的更多信息。 If the line length is equal to 0, and if current_game length is larger than 0 (this check is to add current_game only once in the lists of games) it will add the list to the games list, and it will set it to new empty list. 如果行长等于0,并且current_game长度大于0(此检查是仅在游戏列表中添加current_game一次),则它将列表添加到游戏列表中,并将其设置为新的空白名单。

If you want to cast the strings in the lists to integer you can use the map function when you are splitting the line. 如果要将列表中的字符串转换为整数,可以在分割线时使用map函数。 Here is the same code with casting the string to integers: 这是将字符串转换为整数的相同代码:

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

Finally, to cast the lists in numpy ndim array you can use the array function from numpy. 最后,要将列表转换为numpy ndim数组,可以使用numpy中的array函数。 This solution assumes that after the last game, there will be two empty lines, but it is easy to change that. 该解决方案假定在上一场比赛之后会有两个空行,但是很容易更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM