[英]How to print list inside python print?
我试图建立可变数量的参数记录器,如下所示。 我想这样称呼:
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
,但它似乎正在打印它。
尝试这个:
print(''.join(str(x) for x in [0, 1, 2, 3]))
# 0123
用“ []”括起来
print(tuple([str( x ) for x in (0,1,2,3)]))
输出= ['0','1','2','3']
如果需要,可以转换为元组
print(tuple([str( x ) for x in (0,1,2,3)]))
输出=('0','1','2','3')
并且,如果您想与其他字符串连接,可以使用following并添加您的日志字符串,
元组(str(x)for x in(0,1,2,3)).__ str __()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.