2017-08-01 68 views
0

我想建立一個可變數量的參數記錄器如下。我想這樣稱呼它:如何在Python打印內打印列表?

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

那是因爲你有什麼打印裏面是一個生成器表達式,所以你可以這樣做:'print(list(str(x)for x in(0)) ,1,2,3)))'。 – idjaw

+0

謝謝! 'print(「」.join([str(x)for(in(0,1,2,3)]))''做到了。 – user

回答

1

附上用於與 「[]」

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')

並在你的情況,你想與其他字符串 來連接,您可以使用以下並添加日誌字符串,

元組(STR(X)爲X的(0,1,2,3)).__ STR __()

1

嘗試這種情況:

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

'join'後的括號可以被刪除。 – leaf