更优雅/ Pythonic的方式打印元组元素?

Sha*_*ank 2 python python-2.7

我有一个函数,它返回一大组整数值作为元组.例如:

def solution():
    return 1, 2, 3, 4 #etc.
Run Code Online (Sandbox Code Playgroud)

我想优雅地打印没有元组表示的解决方案.(即数字周围的括号).

我尝试了以下两段代码.

print ' '.join(map(str, solution())) # prints 1 2 3 4
print ', '.join(map(str, solution())) # prints 1, 2, 3, 4
Run Code Online (Sandbox Code Playgroud)

他们都工作,但他们看起来有点难看,我想知道是否有更好的方法这样做.有没有办法"解包"元组参数并将它们传递给printPython 2.7.5中的语句?

我真的很想做这样的事情:

print(*solution()) # this is not valid syntax in Python but I wish it was
Run Code Online (Sandbox Code Playgroud)

有点像元组解包,所以它相当于:

print sol[0], sol[1], sol[2], sol[3] # etc.
Run Code Online (Sandbox Code Playgroud)

除了没有丑陋的索引.有没有办法做到这一点?

我知道这是一个愚蠢的问题,因为我只是想摆脱括号,但我只是想知道是否有一些我不知道的东西.

Ter*_*ryA 8

print(*solution())实际上可以在python 2.7 有效,只需:

from __future__ import print_function
Run Code Online (Sandbox Code Playgroud)

在您的文件的顶部.

你也可以遍历元组:

for i in solution():
    print i,
Run Code Online (Sandbox Code Playgroud)

这相当于:

for i in solution():
    print(i, end= ' ')
Run Code Online (Sandbox Code Playgroud)

如果您曾使用过Python 3或上面的import语句.