2017-01-12 73 views
-3

我要檢查比其他SOS字母給定的字符串中「==,=,<>,是,是不是!」 沒有與Python字符串工作

輸入: - SOSSOTSAR 輸出: - 3 (T,A,R)

s = input() 

c=0 

s=s.replace('SOS','') 

for i in range(len(s)): 
    if(s[i] != "S"): 
     c+=1 
    elif(s[i+1] != "O"): 
     c+=1 
    elif(s[i+2] != "S"): 
     c+=1 
    i+=3  

print(c/3) 
+1

你不符合你的支票。和「SOS以外的其他字母」意味着什麼。 SOS是一個字,而不是字母 –

回答

1

你可以在列表解析一行做到這一點:

s = input() 
print len([x for x in s if x not in 'SOS']) 

但是,如果你想在字獨特字母的數量是不SO,那麼你可以用途:

s = input() 
print len(set([x for x in s if x not in 'SOS'])) 

eg如果你的單詞是SOSOTTAR,第一種方法會給出4(T,T,A,R),而第二種方法會給出3(T,A,R)。

0

您可以像這樣輸入s中的每個字母。

sum(1 for c in s if c not in "SO") 

或更換所有字符串中的SO和使用長度。

len(s.replace("S", "").replace("O", "")) 
0

從你的例子中,你計算字母不等於字母「S」和「O」。

len([i for i in s if not (i == 'S' or i == 'O')]) 
相關問題