简体   繁体   English

Python 挑战 - 行数和列数

[英]Python challenge - rows and columns count

I'm working on a challenge which basically consist on assigning to a set of rows and columns on a table, a value by a function in python. In this case the character's value to be placed on a specific position on the talbe is the "#".我正在研究一个挑战,它基本上包括分配给表上的一组行和列,一个值由 function 在 python 中。在这种情况下,要放置在特定 position 上的字符值是“ #”。 I have been able to make it work, but as you will se on the ouput is that the top which represent the columns list is not totally aligned (top right hand side) to the row line.我已经能够让它工作,但正如您将在输出中看到的那样,代表列列表的顶部并未完全对齐(右上角)到行线。 On the attached screen shot you'll see a more detailed description.在随附的屏幕截图上,您将看到更详细的说明。

Below is the code I am using where I'have been able to get this far.下面是我使用的代码,我已经能够做到这一点。

def bucle_for1(lista):
    x = 0
    
    for i in lista:
        if x <= 9:
            print(x,"", i)
        else:
             print(x, i)
        x += 1

def crear_mundo(d):
    mundo = []
    columna = []
    
    for x in range(d):
        for x in range(d):
            if x <= 9:
                columna.append(" ")
                if x >= 10:
                    columna.append("")
                else:
                    columna.append(" ")
            else:
                columna.append("")
        mundo.append(columna)
        columna = []
    return mundo

def crear_listadocolumnas(d):
    listadocolumnas = []
    for x in range(d):
        listadocolumnas.append(str(x))      
    return listadocolumnas

def muros(mundo):
    muros = [[0,1],[0,2],[1,1],[1,2],[2,1],[2,2],[2,2],
            [3,1],[3,2],[3,3],[3,4],[3,5],[3,6],[3,7],
            [3,8],[3,9],[3,10],[3,11],[4,1],[4,2],[4,3],
            [4,4],[4,5],[4,6],[4,7],[4,8],[4,9],[4,10],
            [4,11],[5,11],[6,11],[6,11],[6,12],[6,13],
            [6,14],[6,15]]
    for x in range(len(muros)):
        f = muros [x][0]
        c = muros [x][1]
        mundo [f][c] = "#"
    return mundo

mundo = crear_mundo(32)
print("  ", crear_listadocolumnas(32))
bucle_for1(muros(mundo))

代码输出

I'm now where I wanted to be with the code (thanks to Display name).我现在在我想使用代码的地方(感谢显示名称)。 Now, I was wodering if you could help me on how to look for the width and len of the following according to the same output. What function should I use so I can calculate the width of column 1 and 2 and also to know the len of the same columns 1 and 2?现在,我想知道你是否可以帮助我如何根据相同的 output 查找以下内容的宽度和 len。我应该使用什么 function 以便我可以计算第 1 列和第 2 列的宽度并知道 len相同的列 1 和 2?

Is this what you were looking for?这是你要找的吗? Basically, if x is greater than 9 (IE 2 characters long, I add基本上,如果 x 大于 9(即 2 个字符长,我添加

    def bucle_for1(lista):
    x = 0

    for i in lista:
        if x <= 9:
            print(x,"", i)
        else:
            print(x, i)
        x += 1



def crear_mundo(d):
    mundo = []
    columna = []
    for x in range(d):
        for x in range(d):
            if x <= 9:
                columna.append(" ")
                if x >= 10:
                    columna.append("")
                else:
                    columna.append(" ")
            else:
                columna.append("")
            if x>9:
                columna[x] = "  "
        mundo.append(columna)
        columna = []
    return mundo



def crear_listadocolumnas(d):
    listadocolumnas = []
    for x in range(d):
        listadocolumnas.append(str(x))
    return listadocolumnas


def muros(mundo):
    muros = [[0,1],[0,2],[1,1],[1,2],[2,1],[2,2],[2,2],
             [3,1],[3,2],[3,3],[3,4],[3,5],[3,6],[3,7],
             [3,8],[3,9],[3,10],[3,11],[4,1],[4,2],[4,3],
             [4,4],[4,5],[4,6],[4,7],[4,8],[4,9],[4,10],
             [4,11],[5,11],[6,11],[6,11],[6,12],[6,13],
             [6,14],[6,15]]
    for x in range(len(muros)):
        f = muros [x][0]
        c = muros [x][1]
        mundo [f][c] = "#"
        if c>9:
            mundo[f][c] += " "
    return mundo

mundo = crear_mundo(32)
print("  ", crear_listadocolumnas(32))
bucle_for1(muros(mundo))

Add single white space after column 31 You can also use this code if you want lines beyond 31 to be a single white space instead of an empty string.在第31 列之后添加一个空格 如果您希望第 31 列之后的行是一个空格而不是一个空字符串,您也可以使用此代码。

def crear_mundo(d):
    mundo = []
    columna = []
    for x in range(d):
        for x in range(d):
            if x <= 9:
                columna.append(" ")
                if x >= 10:
                    columna.append("")
                else:
                    columna.append(" ")
            else:
                columna.append(" ")
            if x>9:
                columna[x] += " "
        mundo.append(columna)
        columna = []
    return mundo

def muros(mundo):
        muros = [[0,1],[0,2],[1,1],[1,2],[2,1],[2,2],[2,2],
                 [3,1],[3,2],[3,3],[3,4],[3,5],[3,6],[3,7],
                 [3,8],[3,9],[3,10],[3,11],[4,1],[4,2],[4,3],
                 [4,4],[4,5],[4,6],[4,7],[4,8],[4,9],[4,10],
                 [4,11],[5,11],[6,11],[6,11],[6,12],[6,13],
                 [6,14],[6,15]]
        for x in range(len(muros)):
            f = muros [x][0]
            c = muros [x][1]
            mundo [f][c] = "#"
            if c>9:
                mundo[f][c] += " "
        return mundo

代码示例

To Match the column length, use this:要匹配列长度,请使用:

def bucle_for1(lista):
    x = 0

    for i in lista:
        if x <= 9:
            print(x,"", i)
        else:
            print(x, i)
        x += 1



def crear_mundo(d):
    mundo = []
    columna = []
    for x in range(d):
        for x in range(d):
            if x <= 9:
                columna.append(" ")
                if x >= 10:
                    columna.append("")
            else:
                columna.append(" ")
            if x>9:
                columna[x] += " "
        mundo.append(columna)
        columna = []
    return mundo



def crear_listadocolumnas(d):
    listadocolumnas = []
    for x in range(d):
        listadocolumnas.append(str(x))
    return listadocolumnas


def muros(mundo):
    muros = [[0,1],[0,2],[1,1],[1,2],[2,1],[2,2],[2,2],
             [3,1],[3,2],[3,3],[3,4],[3,5],[3,6],[3,7],
             [3,8],[3,9],[3,10],[3,11],[4,1],[4,2],[4,3],
             [4,4],[4,5],[4,6],[4,7],[4,8],[4,9],[4,10],
             [4,11],[5,11],[6,11],[6,11],[6,12],[6,13],
             [6,14],[6,15]]
    for x in range(len(muros)):
        f = muros [x][0]
        c = muros [x][1]
        mundo [f][c] = "#"
        if c>9:
            mundo[f][c] += " "
    return mundo

mundo = crear_mundo(32)
print("  ", crear_listadocolumnas(32))
bucle_for1(muros(mundo))

Example:例子: 匹配宽度的示例

Get character length of the printed rows获取打印行的字符长度

def bucle_for1(lista):
    x = 0

    for i in lista:
        y=0
        if x <= 9: y=1
        print(len(str(str(x)+""+ str(i))) + (y))
        if x <= 9:
            print(x,"", i)
        else:
            print(x, i)
        x += 1

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

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