DSpin
DSpin

Reputation: 13

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? i.e:

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

Any help would be appreciated.

Upvotes: 1

Views: 52

Answers (1)

be_good_do_good
be_good_do_good

Reputation: 4441

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()))

appending empty string if c is not greater than 1

Upvotes: 3

Related Questions