[英]Can't seem to add percentage at last in this list?
我有一個這樣的清單:
la = [3, 7, 8, 9, 50, 100]
我試圖將列表轉換為字符串並添加這樣的百分比:
3%, 7%, 8% and lastly 100%
我這樣做:
str1 = '%, '.join(str(e) for e in sorted_list)
它做對了,只是最后沒有百分比,即只有100,但我要100%。 我怎樣才能做到這一點? 謝謝
', '.join('%d%%' % (x,) for x in la)
沒錯,它做什么它應該:將“%”,各部件之間 。 怎么樣...
str1 = '%, '.join(str(e) for e in sorted_list) + '%'
(如果sorted_list
可以為空,則應處理這種情況)
你這個:
print map(lambda n: str(n) + '%', [3, 7, 8, 9, 50, 100])
這將創建所有列表的列表,這些列表都將轉換為帶有百分比的字符串。
如果您仍然想要一個字符串,則可以像下面這樣稍微修改它:
str1 = ', '.join(map(lambda n: str(n) + '%', [3, 7, 8, 9, 50, 100]))
la = [3, 7, 8, 9, 50, 100]
(lambda k: ' and lastly '.join([', '.join(k[:-1]), k[-1]]))(["%s%%" % a for a in la])
# '3%, 7%, 8%, 9%, 50% and lastly 100%'
編輯:小的下標錯誤(然后再次,如Rohit Jain所說的...:D)
python 3.2
str1 = ','.join(str(e)+"%" for e in sorted_list)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.