简体   繁体   English

在python中的特定行上打印有序的dict信息

[英]Print an ordered dict info at specific lines in python

The issue I currently have is that I want to print the value I have stored for my dict using a format that will make it easy for the player in my game to read. 我目前遇到的问题是,我想使用一种格式来打印自己为字典存储的值,该格式将使游戏中的玩家易于阅读。 That way it is easy to interpret what is going on. 这样,很容易解释正在发生的事情。

I have searched around for the last while and I cannot find a solution to what I want. 我搜索了最后一阵子,但找不到我想要的解决方案。 I find things that are similar but do not preform what I need. 我发现相似的东西却没有满足我的需求。 I think i am searching the wrong terms as I am still wet behind the ears at programming. 我认为我在搜索错误的术语,因为我仍然在编程时不知所措。

pprint seems like it could solve the issue but the documentation is hard to decipher for me. pprint似乎可以解决问题,但文档对我来说很难破译。 I have also tried using for loops and while loops to achieve the effect I want but I cannot iterate though my dict correctly to illicit the needed effect. 我也尝试过使用for循环和while循环来达到我想要的效果,但是我无法正确地通过我的命令来迭代所需的效果。

import collections

def generateBoard():
    board = {(x,y):'W' for y in range(1,11) for x in range(1,11)}
    oboard = collections.OrderedDict(sorted(board.items()))
    return oboard

def printBoard(board):
    for x, y in board.items():
        return y

board1 = generateBoard()
printBoard(board1)

Currently all it will do is print out the 'W' over and over again like so. 当前,它将要做的就是像这样一遍又一遍地打印出“ W”。

'W'
'W'
'W'

I would like it to print out like this. 我希望这样打印出来。

'W' 'W' 'W' 'W' 'W' 'W' 'W' 'W' 'W' 'W'
'W' 'W' 'W' 'W' 'W' 'W' 'W' 'W' 'W' 'W'
'W' 'W' 'W' 'W' 'W' 'W' 'W' 'W' 'W' 'W'

Again, I have tried several different looping methods but I cannot figure out how to join the data together. 同样,我尝试了几种不同的循环方法,但无法弄清楚如何将数据连接在一起。 Also because I order the dict it will print out the key:data in the order I want just not the format I want. 另外,因为我对字典进行排序,所以它将以我想要的顺序打印出key:data,而不是我想要的格式。

def printBoard(board):
    while True:
        z = 0
        while x <11:
            p = 0
            z + 1
            while p < 11:
                if board[x,y] == 'W':
                    print y.join(x)
                p + 1

This is one of the ways I tried and it failed miserably. 这是我尝试过的方法之一,但失败了。 It will not let me use an iterator for the if statement which I kinda knew about but I cannot think of a way to get it to go through the x, y tuple to print out the data in a nice format. 它不会让我对我有点了解的if语句使用迭代器,但是我无法想到让它通过x,y元组以一种不错的格式打印数据的方法。

EDIT: 编辑:

I went with tr33hous suggestion of using a class and grc 's example also. 我同意tr33hous也建议使用类和grc的示例。 This code stores the values as I need them, and I can call the print function easily also. 这段代码根据需要存储值,而且我也可以轻松地调用print函数。 I also have it only taking the first character of what ever string I have entered in that position. 我也只采用了我在该位置输入的字符串的第一个字符。 This way later I can just concatenate damaged + ship-name and it will still print the board in a very concise way. 这样一来,我可以将损坏的+船名串联起来,它仍然会以非常简洁的方式打印出电路板。

class Board(object):
    '''Creates a board object with a print board method.'''

    def __init__(self, sides=11):
        self.sides = sides
        self.storedBoard = {(x,y):'W' for y in range(1,self.sides) for x in range(1,self.sides)}

    def print_board(self):
        printableBoard = collections.OrderedDict(sorted(self.storedBoard.items()))
        for y in range(1, self.sides):
            print ' '.join(str(printableBoard[x, y])[0] for x in range(1, self.sides))

I still do not know how I am going to completely implement ship damage or how it is tracked but I think this is a fairly good start. 我仍然不知道我将如何完全实施船舶损坏或如何对其进行跟踪,但是我认为这是一个不错的开始。 Right now though this lets me place the board and then I can run my ship placement script and it changes the 'W' value to the ship description. 现在,尽管这使我可以放置板,然后我可以运行我的船舶布置脚本,并且它将“ W”值更改为船舶描述。

Are you looking for something like this? 您是否正在寻找这样的东西?

for y in range(1, 11):
    print ' '.join(board[x, y] for x in range(1, 11))

Here's a simpler method only using for loops: 这是仅使用for循环的简单方法:

for y in range(1, 11):
    for x in range(1, 11):
        # print the value followed by a single space
        print board[x, y],

    # print a new line
    print

I'd advise using classes for something like this. 我建议对此类使用类。 I have reimplemented your code to reflect the change: 我已经重新实现了您的代码以反映更改:

import collections 
class Board(object):
    def __init__(self, sides=11):
        self.sides = sides
        u_board = {(x,y):'W' for y in range(1,self.sides) for x in
                range(1,self.sides)}
        self.board = collections.OrderedDict(sorted(u_board.items()))

    def print_board(self):
        for y in range(1, self.sides):
            print ' '.join(self.board[x, y] for x in range(1, self.sides))



# Usage

b = Board()
b.print_board()

Output: 输出:

$ python board.py
W W W W W W W W W W
W W W W W W W W W W
W W W W W W W W W W
W W W W W W W W W W
W W W W W W W W W W
W W W W W W W W W W
W W W W W W W W W W
W W W W W W W W W W
W W W W W W W W W W
W W W W W W W W W W

EDIT Modified print function to use @grc 's method 编辑修改的打印功能以使用@grc的方法

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

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