[英]How to add only values of list without brackets in python string formatting
[英]How to print a number list without the brackets and with formatting in Python
我的問題是在一行中沒有括號的打印列表的延續以及如何打印沒有括號的列表
從第二個問題我希望能夠打印特定格式,類似於使用
print(f'{10.1234567:.2f} units')
所以從
Floatlist = [14.715258933890,10.215953824,14.8171645397,10.2458542714719]
print (", ".join(map(str, Floatlist)))
我們得到
14.71525893389, 10.215953824, 14.8171645397, 10.2458542714719
但我想得到
14.72, 10.22, 14.82, 10.25
甚至
14.72 units, 10.22 units, 14.82 units, 10.25 units
再次,使用單行代碼,如上述問題
附言。 我知道將單位放在', '
作為' units, '
我們不會得到最后一項的單位。
謝謝
您只需要將map
操作更改為更精確的操作
floatlist = [14.715258933890,10.215953824,14.8171645397,10.2458542714719]
print(", ".join(map(lambda x : str(round(x, 2)), floatlist))) # for only 2 decimals
print(", ".join(map(lambda x : f'{x:.2f} units', floatlist))) # for 2 decimales + 'units'
注意: 函數名和變量名
將str.format
與str.join
str.format
使用:
Floatlist = [14.715258933890,10.215953824,14.8171645397,10.2458542714719]
print( ', '.join( '{:.2f} units'.format(v) for v in Floatlist ) )
印刷:
14.72 units, 10.22 units, 14.82 units, 10.25 units
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.