簡體   English   中英

使用dict進行Python字符串格式化

[英]Python string formatting with dict

我是python的新手,到目前為止我所做的所有搜索都無法找到答案。 如果有這個問題,請指出我正確的方向。 以下是詳細信息:

我有以下櫃台:

Counter({'storage': 3, 'control': 1})

格式如下:

print "Duplicate IP, {0}, found on {1} nodes.".format(a," and ".join("%s %s" % (c,n) for n,c in dict(counter).items()))

產量:

Duplicate IP, 192.168.56.20, found on 1 control and 3 storage.

有沒有辦法增強這一點,如果計數為1,則不顯示“1”? 即:

Duplicate IP, 192.168.56.20, found on control and 3 storage nodes.

任何幫助,將不勝感激。

print "Duplicate IP, {0}, found on {1} nodes.".format(a," and ".join("%s %s" % (c if c > 1 else '',n) for n,c in dict(counter).items()))

如果c不大於1,則附加空字符串

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM