繁体   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