Python 其中包括'\n';?
我正试图获得以下信息:Python 其中包括'\n';?,python,python-2.7,python-3.x,Python,Python 2.7,Python 3.x,我正试图获得以下信息: [A1, B1, C1] [A2, B2, C2] [A3, B3, C3] 使用列表理解。 我已经知道了,但它只在一行中打印内容: listeHoriz = ['A','B','C'] listeVert = ['1','2','3'] def generatechess (): return [[z+y for z in listeHoriz ] for y in listeVert] print(generatechess()) 我拼命想把“\n”放
[A1, B1, C1]
[A2, B2, C2]
[A3, B3, C3]
使用列表理解。
我已经知道了,但它只在一行中打印内容:
listeHoriz = ['A','B','C']
listeVert = ['1','2','3']
def generatechess ():
return [[z+y for z in listeHoriz ] for y in listeVert]
print(generatechess())
我拼命想把“\n”放在某个地方,但是找不到正确的位置,你知道我应该把它放在哪里吗?(使用列表理解!)您可以这样做:
listeHoriz = ['A','B','C']
listeVert = ['1','2','3']
def generatechess():
combinations = [str([z+y for z in listeHoriz ]) for y in listeVert]
return ("\n").join(combinations)
print(generatechess())
或者简单地说:
listeHoriz = ['A','B','C']
listeVert = ['1','2','3']
def generatechess():
return [str([z+y for z in listeHoriz ]) for y in listeVert]
print("\n".join(generatechess()))
您可以这样做:
listeHoriz = ['A','B','C']
listeVert = ['1','2','3']
def generatechess():
combinations = [str([z+y for z in listeHoriz ]) for y in listeVert]
return ("\n").join(combinations)
print(generatechess())
或者简单地说:
listeHoriz = ['A','B','C']
listeVert = ['1','2','3']
def generatechess():
return [str([z+y for z in listeHoriz ]) for y in listeVert]
print("\n".join(generatechess()))
在
def
功能中尝试以下操作:
def generatechess():
print(*[[z+y for z in listeHoriz ] for y in listeVert], sep = '\n')
generatechess()
['A1', 'B1', 'C1']
['A2', 'B2', 'C2']
['A3', 'B3', 'C3']
如果要在Python 2.7中尝试此操作,必须将
from uuu future\uuuu导入print_函数
指定为第一个import语句,以便将print
用作函数。在def
函数中尝试此操作:
def generatechess():
print(*[[z+y for z in listeHoriz ] for y in listeVert], sep = '\n')
generatechess()
['A1', 'B1', 'C1']
['A2', 'B2', 'C2']
['A3', 'B3', 'C3']
如果您想在Python 2.7中尝试这一点,您必须指定
from uuuu future uuuuuuuu导入print_函数
作为第一个导入语句,以便将print
用作函数。您的代码实际上使用外部[
和]
和引号'.
围绕A1
打印整个变量,B1
,…,这是你想要的还是你确实想要你给出的输出?你的代码实际上是用外部的[
和]
打印整个变量,并在A1
,B1
周围加上引号,…,这是你想要的还是你确实想要你给出的输出?我认为这是最好的答案,我会保留返回,而不是执行print(*generatechess(),sep='\n')
我认为这是最好的答案,我会保留返回,而执行print(*generatechess(),sep='\n')