简体   繁体   中英

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. 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? 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,则附加空字符串

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM