代码之家  ›  专栏  ›  技术社区  ›  Evandro Coan

如何在python打印中打印列表?

  •  0
  • Evandro Coan  · 技术社区  · 8 年前

    我试图构建一个可变数量的参数记录器,如下所示。我想这样称呼它:

    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 回复  |  直到 8 年前
        1
  •  1
  •   DexJ    8 年前

    用“[]”附上您的for

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

    输出=['0',1',2',3']

    如果需要,可以转换为元组

    在您的例子中,您希望与其他字符串连接 您可以使用以下命令并将其添加到日志字符串中,

        2
  •  1
  •   leaf    8 年前

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