word=input('something:')
sum = 0
chars=set(list(word)) #get the set of unique characters
for item in chars: #iterate over the set and output the count for each item
if word.count(char)>1:
sum+=word.count(char)
print('{}|{}'.format(item,str(word.count(char)))
print('Total:'+str(sum))
原始问题:
order of repetition does not matter
你可以用字母总数减去唯一字母的数目。^应用于字符串的{a1}将返回一个唯一的字母集合。在
或者可以使用^{} ,从每个值中减去1,然后
^{pr2}$sum
:新问题:
repetitions must be sequential
可以使用^{} 对顺序相同的字符进行分组:
要避免嵌套的} :
sum
调用,可以使用^{试试这个:
编辑:
增加了总重复次数
如果需要,可以使用正则表达式:
这将正确生成
6
,并使用.span()
函数。表达式是 ^{pr2}$
它捕获一个单词字符(其中一个是
a-zA-Z0-9_
),并尽可能频繁地重复它。有关重复模式,请参见a demo on regex101.com。
如果要匹配任何字符(即不仅仅是单词字符),请将表达式更改为:
见another demo on regex101.com。在
相关问题 更多 >
编程相关推荐