2017-06-20 140 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