【问题标题】:Function when returning a paragraph python [closed]返回段落python时的功能[关闭]
【发布时间】:2019-05-18 16:59:58
【问题描述】:

我正在定义一个函数来返回一个段落字符串,但是当我返回它,然后打印它时,它会返回这个:

(' | a | b | c | d | e | f | g | h | i | j |\n --+---+---+---+---+--- +---+---+---+---+---+\n 10|', ' | | | ♥ | | | ♥ | | | ', '|\n --+-- -+---+---+---+---+---+---+---+---+---+\n 9 |', ' | | | | | | | | | ', '|\n --+---+---+---+---+---+---+---+---+---+- --+\n 8 |', ' | | | | | | | | ', '|\n --+---+---+---+---+---+-- -+---+---+---+---+\n 7 |', '♥ | | | | | | | | | ♥', '|\n --+---+- --+---+---+---+---+---+---+---+---+\n 6 |', ' | | | | | | | | | ', '|\n --+---+---+---+---+---+---+---+---+---+---+ \n 5 |', ' | | | | | | | | | ', '|\n --+---+---+---+---+---+---+- --+---+---+---+\n 4 |', '♠ | | | | | | | | | ♠', '|\n --+---+---+ ---+---+---+---+---+---+---+---+\n 3 |', ' | | | | | | | | ', '|\n --+---+---+---+---+---+---+---+---+---+---+\n 2 |', ' | | | | | | | | | ', '|\n --+--- +---+---+---+---+---+---+---+---+---+\n 1 |', ' | | | ♠ | | | ♠ | | | ', '|\n --+---+---+---+---+---+---+---+---+---+---+' )

但是如果我在函数内部打印它,而不是返回它。这就是结果。

  | a | b | c | d | e | f | g | h | i | j |
--+---+---+---+---+---+---+---+---+---+---+
10|   |   |   | ♥ |   |   | ♥ |   |   |   |
--+---+---+---+---+---+---+---+---+---+---+
9 |   |   |   |   |   |   |   |   |   |   |
--+---+---+---+---+---+---+---+---+---+---+
8 |   |   |   |   |   |   |   |   |   |   |
--+---+---+---+---+---+---+---+---+---+---+
7 | ♥ |   |   |   |   |   |   |   |   | ♥ |
--+---+---+---+---+---+---+---+---+---+---+
6 |   |   |   |   |   |   |   |   |   |   |
--+---+---+---+---+---+---+---+---+---+---+
5 |   |   |   |   |   |   |   |   |   |   |
--+---+---+---+---+---+---+---+---+---+---+
4 | ♠ |   |   |   |   |   |   |   |   | ♠ |
--+---+---+---+---+---+---+---+---+---+---+
3 |   |   |   |   |   |   |   |   |   |   |
--+---+---+---+---+---+---+---+---+---+---+
2 |   |   |   |   |   |   |   |   |   |   |
--+---+---+---+---+---+---+---+---+---+---+
1 |   |   |   | ♠ |   |   | ♠ |   |   |   |
--+---+---+---+---+---+---+---+---+---+---+

这是代码,希望对你有帮助

tableron=[]
for i in range(10):
    tableron.append([" "]*10)

columnas=["a","b","c","d","e","f","g","h","i","j"]

def cargar_tablero():
    tablero=[]
    archivo=open("tablero.txt","r")
    for line in archivo:
        line=line.strip()
        tablero.append(line.split(","))
    return tablero

def nuevo_tablero():
    tablero=[["a4","d1","g1","j4"],["a7","d10","g10","j7"],[],["1"]]
    return tablero

def tablero_to_string(tablero):
        lista=tablero[0]
        for m in lista:
            tableron[int(m[1::])-1].pop(columnas.index(m[0]))
            tableron[int(m[1::])-1].insert(columnas.index(m[0]),"♠")

        lista=tablero[1]
        for m in lista:
            tableron[int(m[1::])-1].pop(columnas.index(m[0]))
            tableron[int(m[1::])-1].insert(columnas.index(m[0]),"♥")


        if tablero[3]!= None:
            lista=tablero[2]
            for m in lista:
                tableron[int(m[1::])-1].pop(columnas.index(m[0]))
                tableron[int(m[1::])-1].insert(columnas.index(m[0]),"*")          



        print ("""      | a | b | c | d | e | f | g | h | i | j |
    --+---+---+---+---+---+---+---+---+---+---+
    10|"""," | ".join(tableron[9]),"""|
    --+---+---+---+---+---+---+---+---+---+---+
    9 |"""," | ".join(tableron[8]),"""|
    --+---+---+---+---+---+---+---+---+---+---+
    8 |"""," | ".join(tableron[7]),"""|
    --+---+---+---+---+---+---+---+---+---+---+
    7 |"""," | ".join(tableron[6]),"""|
    --+---+---+---+---+---+---+---+---+---+---+
    6 |"""," | ".join(tableron[5]),"""|
    --+---+---+---+---+---+---+---+---+---+---+
    5 |"""," | ".join(tableron[4]),"""|
    --+---+---+---+---+---+---+---+---+---+---+
    4 |"""," | ".join(tableron[3]),"""|
    --+---+---+---+---+---+---+---+---+---+---+
    3 |"""," | ".join(tableron[2]),"""|
    --+---+---+---+---+---+---+---+---+---+---+
    2 |"""," | ".join(tableron[1]),"""|
    --+---+---+---+---+---+---+---+---+---+---+
    1 |"""," | ".join(tableron[0]),"""|
    --+---+---+---+---+---+---+---+---+---+---+""")


tablero=nuevo_tablero()
tablero_to_string(tablero)

【问题讨论】:

  • 你的问题是什么,你能展示你的代码吗?
  • 对不起,我刚刚更新了它
  • 问题是如何让第一个输出看起来像第二个,但返回一个字符串?

标签: python string algorithm paragraph


【解决方案1】:

你的长字符串实际上不是一个字符串,而是一个字符串序列。如果您将它们传递给print,它们都会依次打印。如果你返回它们,它们就会变成一个元组。如果你尝试打印一个元组,你会得到不同的输出。

您可以通过加入它来将整个批次作为一个字符串返回。

return ' '.join(["""      | a | b | c | d | e | f | g | h | i | j |
    --+---+---+---+---+---+---+---+---+---+---+
    10|"""," | ".join(tableron[9]),"""|
    --+---+---+---+---+---+---+---+---+---+---+
    9 |"""," | ".join(tableron[8]),"""|
    --+---+---+---+---+---+---+---+---+---+---+
    8 |"""," | ".join(tableron[7]),"""|
    --+---+---+---+---+---+---+---+---+---+---+
    7 |"""," | ".join(tableron[6]),"""|
    --+---+---+---+---+---+---+---+---+---+---+
    6 |"""," | ".join(tableron[5]),"""|
    --+---+---+---+---+---+---+---+---+---+---+
    5 |"""," | ".join(tableron[4]),"""|
    --+---+---+---+---+---+---+---+---+---+---+
    4 |"""," | ".join(tableron[3]),"""|
    --+---+---+---+---+---+---+---+---+---+---+
    3 |"""," | ".join(tableron[2]),"""|
    --+---+---+---+---+---+---+---+---+---+---+
    2 |"""," | ".join(tableron[1]),"""|
    --+---+---+---+---+---+---+---+---+---+---+
    1 |"""," | ".join(tableron[0]),"""|
    --+---+---+---+---+---+---+---+---+---+---+"""])

或者你可以用连接而不是逗号来构造它。

一旦你将它作为一个字符串而不是一个元组返回,打印它应该可以正常工作。

【讨论】:

  • 但是 join 不适用于 21 个参数。
  • join 如果你把' '.join([x,y,z]) 工作,因为参数被包装到一个参数(一个列表)。因此,我的答案中的方括号。
【解决方案2】:

terminaltables

#!/usr/bin/env python
# -*- coding: utf-8 -*-


from terminaltables import AsciiTable
table_data=[
['','a','b','c','d','e','f','g','h','i','j'],
['10','','','','♥','','','♥','','',''],
['9','','','','','','','','','',''],
['8','','','','','','','','','',''],
['7','♥','','','','','','','','','♥'],
['6','','','','','','','','','',''],
['5','','','','','','','','','',''],
['4','♠','','','','','','','','','♠'],
['3','','','','','','','','','',''],
['2','','','','','','','','','',''],
['1','','','♠','','','♠','','','',''],
]
table = AsciiTable(table_data)
table.inner_row_border = True
print table.table

输出与您的相同。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-08
    • 2013-01-10
    • 2011-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多