找出字符串中出現次數最多的字符,并輸出其出現的位置 兩種方法

print("找出字符串中出現次數最多的字符,并輸出其出現的位置")

2.png

str = "abbcccdddd"
第一種方法:
max = 0
i = 0
for a in str:
if max < str.count(a):
max = str.count(a)
while str.find(a,i) != -1:
i = str.find(a,i) + 1
print(i)
print(a)


3.png

第二種方法

str = "abbcccdddd"
word = ""
position = []
max = 0
dict_s = {}
for i in str:
if max > str.count(i):
max = str.count(i)
word = i
print(word)

for j in range(len(str)):
if str[j] == word:
position.append(j)
dict_s[word] = position
print(position)
print(dict_s)

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。