2014-01-20 35 views
1

我對編程非常陌生。我一直試圖讓string.count函數工作。 這是我到目前爲止有:讓str.count()在python上工作

def trial(): 
    a = raw_input ("Enter a string:").lower() 
    print a 
    b = raw_input ("Enter a substring:").lower() 
    print b 

    print "The total character count is:" + a.count('b') 

它仍然沒有給我計數。它不斷給我<built-in method count of str object at 0x0181B500>。 我想我做錯了什麼。我意識到這可能是一個簡單的問題,但我是一個初學者。我讀過你必須使用str.count(sub [,start [,end]]),但開始和結束是什麼意思?我閱讀了描述,但我並沒有真正明白。

任何形式的幫助將不勝感激!

+0

我真的不明白你的問題。你能提供一個樣本輸入和期望的輸出嗎? – aIKid

+4

我不相信這是你的實際代碼,因爲這段代碼不打印那個輸出。請張貼更具代表性的內容。 –

+0

'a.count('b')'必須是'str(a.count('b'))'讓打印工作。甚至是'str(a.count(b))'來讓搜索起作用。 – Mehdi

回答

1

您收到此消息來自運行的是a.countb.count。如果不將對象包含在對象方法中,則直接按消息中所示訪問方法對象。

您的算法不正確。 a.count('b')將返回a中出現字符'b'的總次數。您的打印消息顯示它顯示「總字符數」。要做到這一點,你可以使用len()函數。要獲得a中的字符總數,您將運行len(a),並且對於b,您將運行len(b)。如果您嘗試獲得總數,則您將運行len(a) + len(b)len(a + b),如果您嘗試獲取b的總次數a,那麼您將運行a.count(b)

+0

它的工作!非常感謝幫助和簡單的解釋! :) – user3213742

2

您還沒有保存您的代碼或以其他方式使用print a.count而不是print a.count()。要打印的count方法表示的字符串對象,前者手段,因此打印出:

<built-in method count of str object at 0x0181B500> 

至於sub,start,end的意思,我可能會建議the Python documentation for string objects?

string.count(s, sub[, start[, end]]) 

返回字符串s [start:end]中子字符串sub(不重疊)出現的次數。開始和結束的默認值以及負值的解釋與切片相同。

從本質上說,這意味着:

"Some long text right about here".count("o",5,9) 

只會計算o S中的數子串"Some long text right about here"[5:9]這是equivilent到"long",所以會返回1

2

您是否試圖打印在a中發生b多少次?通過傳遞'b',您將獲得字符串b中出現的字符次數,而不是b變量的內容。另外,你試圖連接一個字符串和一個int。請進行以下調整。

def trial(): 
    a = raw_input ("Enter a string:").lower() 
    print a 
    b = raw_input ("Enter a substring:").lower() 
    print b 

    print "The total character count is:" + str(a.count(b)) 
1

在這裏,我們無法理解你的問題,你希望沒有字符被計數或統計單個字符的出現。 下面的代碼計數所涉及的字符數量。

from collections import Counter 
str = "Mary had a little lamb" 
counter = Counter(str) 
print Counter[str] 

下面的代碼的唯一字符

​​
1

出於測試目的,它是很好的做法,單獨的邏輯和I/O

例如:。

def trial(a, b): 
    return "The total character count is:" + a.count('b') 

現在,您可以快速測試這樣

>>> trial("foo bar", "oo") 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "<stdin>", line 2, in trial 
TypeError: cannot concatenate 'str' and 'int' objects 

哦,對了,首選的方法是使用.format()

def trial(a, b): 
    return "The total character count is: {}".format(a.count('b')) 

現在你可以再次測試

>>> trial("foo bar", "oo") 
'The total character count is: 1' 

您的原始錯誤未生成由你的問題中的代碼。你可能有這樣的事情

def trial(): 
    a = raw_input ("Enter a string:").lower() 
    print a 
    b = raw_input ("Enter a substring:").lower() 
    print b 

    print "The total character count is:", a.count 
0

我用python 3.3.1回答了這個問題。

根據你的榜樣

def trial() : 
    a = input ("Enter a string:").lower() 
    #takes your input in lower case and assign to variable a 
    print (a) 
    #print above input (content of a) 
    b = input ("Enter a substring:").lower() 
    #takes your input in lower case and assign to variable b 
    print (b) 
    #print above input (content of b) 

    print ("The total character count is:", a.count(b)) 
    #this will give you how many times content of 'b' repeat in content of 'a'. 

trial() 

str.count(子,開始,結束) - > 你的榜樣 'STR' 是等於 'A'。 'sub'等於'b'。沒有給出開始和結束。所以默認開始是'a'的索引0,默認結束是'a'的最後一個索引。假設你的a ='beautiful_mind_sing'和b ='i'。如果你給a.count(b,1,13)。即使有三個'我'的答案將2因爲這裏的開始和結束索引是1和13.因此a.count()僅搜索索引1到13。範圍'e'和'd' - >'b * e * autiful_min * d * _sing'之間。

附加例如:

first_line='Hello Sandu how are you? Hello Anu how are you? Hello Sa how are you?' 
searching_word='are' 

print("Number of searching_word 'are' repeate in the first_line is: ", first_line.count(searching_word,3,50)) 

答案:

searching_word的數量 '是' repeate在first_line是:2

參考:http://www.tutorialspoint.com/python/string_count.htm