如何在python print中打印列表?

时间:2017-08-01 01:16:55

标签: python python-2.7 python-3.x debugging variadic-functions

我正在尝试构建一个可变数量的参数logger,如下所示。我想称之为:

log( 1, "Index: ", request_index, ", ", section )

def log(level, *msg) :

    global print_debug_lastTime
    currentTime = datetime.datetime.now().microsecond

    # You can access global variables without the global keyword.
    if g_debug_level & level != 0:

        print( "[DEBUG] " \
                + "%02d" % datetime.datetime.now().hour + ":" \
                + "%02d" % datetime.datetime.now().minute + ":" \
                + "%02d" % datetime.datetime.now().second + ":" \
                + str( currentTime ) \
                + "%7d " % ( currentTime - print_debug_lastTime ) \
                + for m in msg )

但是我无法打印可变数量的参数。最初我试图在解释器上运行这个简单的代码:

>>> print( str( x ) for x in (0,1,2,3) )
<generator object <genexpr> at 0x6ffffe5e550>

我希望它能够打印0123,但它可能看起来正在打印它。

2 个答案:

答案 0 :(得分:1)

试试这个:

print(''.join(str(x) for x in [0, 1, 2, 3]))
# 0123

答案 1 :(得分:1)

用“[]”

附上你的
print(tuple([str( x ) for x in (0,1,2,3)]))
  

output = ['0','1','2','3']

如果你想要转换为元组

print(tuple([str( x ) for x in (0,1,2,3)]))
  

输出=('0','1','2','3')

在您的情况下,您想要与其他字符串连接 您可以使用以下内容并添加日志字符串

  

元组(str(x)for x in(0,1,2,3)).__ str __()