
看的都是python字串中的每一個字母出現的次數,但是對于固定的兩個字母出現的次數的python代碼不知道怎么去寫



還有幾道題是有關函式控制流和data structure的,希望可以幫忙看看,感謝
uj5u.com熱心網友回復:
。。一堆圖片太凌亂了,提煉成簡短的問題就好了第一個猜測想統計兩個字母出現次數 "abdhe he".count("he") 這樣就可以,但是不知道為什么你說“每一個字母出現的次數”
uj5u.com熱心網友回復:
def count_he(str):
return str.count('he')
print(count_he('abc he ho'))
print(count_he('ABChe he'))
print(count_he('hehe'))
# OUTPUT:
# 1
# 2
# 2
uj5u.com熱心網友回復:
def count_he(s):
return s.count('he')
def div7_not5(a, b):
return ','.join([str(i) for i in range(a, b+1) if i % 7 == 0 and i % 5 != 0])
def build_freq_word_dict(s):
from collections import Counter
di = Counter(s.split())
g = {i: [k for k, v in di.items() if v == i] for i in range(1, max(di.values()) + 1)}
return g
def reduce_num(n):
tn = n
if tn < 2 :
return '1'
l = []
i = 2
while tn >1:
if tn % i == 0:
l.append(str(i))
tn = tn // i
else:
i += 1
return str(n) + '=='+'*'.join(l)
uj5u.com熱心網友回復:

感謝你的回復!但是第四個代碼我運行出來顯示了上面如圖的錯誤,可以幫我看看是怎么回事嗎?
uj5u.com熱心網友回復:
把return str(n)......中的==改成=
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/225680.html
