返回段落时的功能 python
Function when returning a paragraph python
我正在为 return 段落字符串定义一个函数,但是当我 return 它,然后打印它时,它 return 是这样的:
(' | 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 --+---+---+---+---+---+---+---+---+---+---+' )
但不是 returning 它,如果我在函数内部打印它。这是结果。
| 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)
你的长字符串实际上不是一个字符串,而是一个字符串序列。如果将它们传递给 print
,它们都会依次打印出来。如果你 return 他们,他们就会变成一个元组。如果你尝试打印一个元组,你会得到不同的输出。
您可以 return 通过连接将整个地段作为一个字符串。
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]),"""|
--+---+---+---+---+---+---+---+---+---+---+"""])
或者您可以使用连接而不是逗号来构造它。
一旦你return将它作为一个字符串而不是一个元组,打印它应该可以正常工作。
#!/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
输出与您的相同。
我正在为 return 段落字符串定义一个函数,但是当我 return 它,然后打印它时,它 return 是这样的:
(' | 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 --+---+---+---+---+---+---+---+---+---+---+' )
但不是 returning 它,如果我在函数内部打印它。这是结果。
| 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)
你的长字符串实际上不是一个字符串,而是一个字符串序列。如果将它们传递给 print
,它们都会依次打印出来。如果你 return 他们,他们就会变成一个元组。如果你尝试打印一个元组,你会得到不同的输出。
您可以 return 通过连接将整个地段作为一个字符串。
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]),"""|
--+---+---+---+---+---+---+---+---+---+---+"""])
或者您可以使用连接而不是逗号来构造它。
一旦你return将它作为一个字符串而不是一个元组,打印它应该可以正常工作。
#!/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
输出与您的相同。