![](/img/trans.png)
[英]How to print a list of characters in python without brackets, commas, or quotes?
[英]How to print a list of tuples without commas in Python?
我在python中有兩個列表。
a = [1, 2, 4, 8, 16]
b = [0, 1, 2, 3, 4]
第三個列表c
是它們的zip
。
c = zip(a, b)
或者只是我有一個這樣的元組列表:
c = [(1, 0), (2, 1), (4, 2), (8, 3), (16, 4)]
我想在括號后不帶逗號的情況下打印列表c
。 有沒有辦法在Python中做到這一點?
我想這樣打印列表c
:
[(1, 0) (2, 1) (4, 2) (8, 3) (16, 4)]
print('[%s]' % ' '.join(map(str, c)))
印刷品:
[(1, 0) (2, 1) (4, 2) (8, 3) (16, 4)]
供您輸入。
基本上,您可以利用以下事實:您使用的是元組的自然字符串表示形式,並且僅使用空格連接。
print('[' + ' '.join([str(tup) for tup in c]) + ']')
使用列表推導以字符串形式創建元組列表。 然后將它們合並,並添加方括號以使其看起來像您想要的那樣。
將最終結果創建為字符串可以嗎?
c_no_comma = ''
for element in c:
c_no_comma += str(element) + ' '
c_no_comma = '[ ' + c_no_comma + ']'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.