[英]Python - load unknown data as n-dim matrix
我有一个数据文件,其中包括一个未知棋盘游戏的“快照”,例如tictactoe / dama / chess / go..etc。 但是我不知道游戏的参数,例如棋盘的尺寸,棋子的类型等等。
最简单的情况是抽动,所以以它为例。 件和空白字段用数字(-n,-n + 1 .. 0,+ n-1 .. + n ..)表示
开始:
在这种简单情况下,每次移动(x,O用1或-1表示,空字段为0。)。 最后,我将得到一组由两个空行分隔的3x3矩阵。
我如何将数据读入ndim数组 ([length_of_game] [board_width] [board_length], 而无需手动添加有关游戏的口径/长度的信息) ?
我只知道我的棋盘尺寸未知,不同的棋子用不同的数字表示,而快照则表示游戏的发展。
您可以执行此操作的一种方法是逐行分析文件。 将行用空格分隔(假设一行中的数字由空格分隔),然后将结果列表添加到将容纳所有行(行数据)的另一列表中(称为此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,如果大于0,则首先将其剥离(从行尾删除任何空白),然后按空白将其分割。 您可以在此处阅读有关split函数的更多信息。 如果行长等于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函数。 该解决方案假定在上一场比赛之后会有两个空行,但是很容易更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.