[英]Python: How to return a string with a certain formatting for a nested list
So I am trying to make a text-based adventure game, and it has a map system managed using a nested list, like so: 因此,我尝试制作一个基于文本的冒险游戏,它具有一个使用嵌套列表管理的地图系统,如下所示:
self.map_ = [
['O', 'O', 'O'],
['O', 'O', 'O'],
['O', 'O', 'O']
]
I have the map inside a class called Map()
that has functions to create the map, add and change cells, and so on. 我将地图包含在名为Map()
的类中,该类具有创建地图,添加和更改单元格等的功能。
However, I am not sure how to print the map. 但是,我不确定如何打印地图。 I would like to create a __str__()
function to print the map, but I am unsure of how to do so. 我想创建一个__str__()
函数来打印地图,但是我不确定该怎么做。 I would like it to be printed out like this, with spaces separating the elements on the X axis and a line break separating the Y axis: 我希望这样打印出来,在X轴上用空格隔开元素,在Y轴上用换行符隔开:
O O O
O O O
O O O
I have seen examples that look like this: 我看过这样的示例:
for row in map_:
print ' '.join(row)
...and this example prints the map in exactly the format I would like. ...并且此示例以我想要的格式打印地图。
However, I want to turn this into a __str__()
function, where it returns a combination of ' '.join(row)
(printing a space between each row element) and '\\n'.join(map_)
(printing a newline between each row). 但是,我想将其转换为__str__()
函数,在此函数返回' '.join(row)
(在每个行元素之间打印一个空格)和'\\n'.join(map_)
(打印换行符)的组合每行之间)。
Of course, I would like the elements to be in the correct order (upper left on the printed view corresponds to map_[0][0]
etc). 当然,我希望元素的顺序正确(在打印视图的左上方对应于map_[0][0]
等)。
Is there a way I can do this with the __str__()
function, or do I have to resort to using the printing function above? 有什么方法可以使用__str__()
函数做到这一点,还是必须诉诸于使用上面的打印功能?
How about this, using a list comprehension ? 使用列表理解如何呢?
"\n".join([" ".join(row) for row in map_])
For example: 例如:
In [1]: map_ = [
...: ['O', 'O', 'O'],
...: ['O', 'O', 'O'],
...: ['O', 'O', 'O']
...: ]
In [2]: print "\n".join([" ".join(row) for row in map_])
O O O
O O O
O O O
And to more directly answer your question about using __str__()
: 并直接回答有关使用__str__()
:
def __str__(self):
return "\n".join([" ".join(row) for row in self.map_])
use space to join each row element, and use \\n
to join each row 使用空格连接每行元素,并使用\\n
连接每行
def __str__(self):
return "\n".join([" ".join(x) for x in self.map_])
Slightly faster solution than the list comprehension approach, though it would only matter at all if the board sizes were huge: 比清单理解方法快一点的解决方案,尽管只有板子尺寸很大时才重要:
def __str__(self):
return '\n'.join(map(' '.join, self.map_))
map
with a C built-in function (which str.join
is) can beat an equivalent list comprehension by a decent margin by pushing more work to the C layer on the CPython reference interpreter. map
与C内置函数(其str.join
是)能够通过压入更多的工作,以在CPython的参考解释器C层通过一个体面余量击败的等效列表解析。
Try this: 尝试这个:
class Map:
def __init__(self):
self.map_ = [
['O', 'O', 'O'],
['O', 'O', 'O'],
['O', 'O', 'O']
]
def __str__(self):
string = ""
for i in self.map_:
for j in i:
string += j + " "
string += "\n"
return string
def change(self, x, y): # For change correctly invert the order
self.map_[y][x] = "A"
def main():
mapa = Map()
print(mapa)
if __name__ == '__main__':
main()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.