2017-06-20 138 views
1

我是相當新的python和迄今爲止我所做的所有搜索我無法找到答案。如果已經得到解答,請指出正確的方向。下面是詳細信息: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. 

任何幫助,將不勝感激。

回答

3
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

+1

這個工作。感謝您的答覆。 – DSpin