简体   繁体   English

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

[英]Python string formatting with dict

I am fairly new to python and with all the searching I have done thus far I am unable to find an answer. 我是python的新手,到目前为止我所做的所有搜索都无法找到答案。 If this has been answered please point me in the right direction. 如果有这个问题,请指出我正确的方向。 Here are the details: 以下是详细信息:

I have the following counter: 我有以下柜台:

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

The formatting is as follows: 格式如下:

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

which yields: 产量:

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

Is there a way to enhance this such that if the count is 1 the '1' is not displayed? 有没有办法增强这一点,如果计数为1,则不显示“1”? ie: 即:

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

Any help would be appreciated. 任何帮助,将不胜感激。

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