我需要一个程序来检查字符串中是否有两个或更多相同的字符。不必像bb一样彼此相邻,就像鲍勃一样。他们只需要有一次或多次相同的charactar。
我现在所做的不起作用,因为它自动说酷有两个相同的特征:
import collections
word = 'cool'
c = collections.Counter(word)
if c>1:
>>>>print (word,'has two of the same charactars:')
else:
>>>>print (word,'has no same charactars:')
答案 0 :(得分:0)
你几乎就在那里,只需要计数器的.values()方法。以下测试两种情况。
import collections
def dupchar(word):
c = collections.Counter(word)
return any(i >= 2 for i in c.values())
for word in 'hot', 'cool':
print('{} has {} same characters'.format(word,
'two of the' if dupchar(word) else 'no'))
打印
hot has no same characters
cool has two of the same characters