Python - 将未知数据加载为 n-dim 矩阵
Python - load unknown data as n-dim matrix
我有一个数据文件,其中包含未知棋盘游戏的"snapshots",例如tictactoe/dama/chess/go..等。 但是我不知道游戏的参数,比如棋盘的尺寸,棋子的类型等等
最简单的例子就是井字,我们就拿它来举例吧。
块和空字段表示为数字 (-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 大小的信息?
我只知道我有一个未知大小的棋盘,不同的棋子用不同的数字表示,快照代表了游戏的演变。
执行此操作的一种方法是逐行解析文件。用白色 space 分割线(假设一行中的数字用白色 space 分隔)并将结果列表添加到另一个列表中(我们称之为 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,如果是,则先进行条带化(去掉行尾的白色space),然后拆分它由白色 space。您可以阅读有关拆分功能的更多信息 here。如果线长等于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 函数。
这个解决方案假设在最后一场比赛之后,会有两个空行,但很容易改变。
我有一个数据文件,其中包含未知棋盘游戏的"snapshots",例如tictactoe/dama/chess/go..等。 但是我不知道游戏的参数,比如棋盘的尺寸,棋子的类型等等
最简单的例子就是井字,我们就拿它来举例吧。 块和空字段表示为数字 (-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 大小的信息?
我只知道我有一个未知大小的棋盘,不同的棋子用不同的数字表示,快照代表了游戏的演变。
执行此操作的一种方法是逐行解析文件。用白色 space 分割线(假设一行中的数字用白色 space 分隔)并将结果列表添加到另一个列表中(我们称之为 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,如果是,则先进行条带化(去掉行尾的白色space),然后拆分它由白色 space。您可以阅读有关拆分功能的更多信息 here。如果线长等于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 函数。 这个解决方案假设在最后一场比赛之后,会有两个空行,但很容易改变。