7
A
回答
26
爲什麼不讀第一的文檔,這是非常簡單的:
>>> "The big brown fox is brown".count("brown")
2
+1
簡單而優雅! – AutomatedTester 2009-11-03 11:30:03
18
有一兩件事值得學習,如果你是一個Python初學者是如何使用interactive mode來解決這個問題的。首先要學習的是dir
function,它會告訴你一個對象的屬性。
>>> mystring = "The big brown fox is brown"
>>> dir(mystring)
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__
ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__g
t__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__
', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '
__rmul__', '__setattr__', '__str__', 'capitalize', 'center', 'count', 'decode',
'encode', 'endswith', 'expandtabs', 'find', 'index', 'isalnum', 'isalpha', 'isdi
git', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lst
rip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit'
, 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', '
translate', 'upper', 'zfill']
請記住,在Python中,方法也是屬性。所以,現在他使用help
function來電諮詢,看起來有前途的方法大約一個:
>>> help(mystring.count)
Help on built-in function count:
count(...)
S.count(sub[, start[, end]]) -> int
Return the number of non-overlapping occurrences of substring sub in
string S[start:end]. Optional arguments start and end are interpreted
as in slice notation.
這顯示了該方法的docstring - 幫助文本,你應該獲得在投入自己的方法太習慣。
+0
+1我不知道幫助方法!謝謝 – systempuntoout 2010-03-17 11:22:44
相關問題
- 1. 計算字符串中某個點之前發生的次數
- 2. 計數字符串發生的次數
- 3. 計算字符串中某個字母的出現次數
- 4. 我如何計算列表中每個字符串的位置與每個字符串的發生次數?
- 5. 字符串發生計數算法
- 6. 如何計算字符串中某個字符的頻率?
- 7. 計算SQL字符串值中某個字符的出現次數?
- 8. ES6/lodash計算字符串中某個字符的出現次數
- 9. Spotfire:計算字符串中某個字符的數量
- 10. 我如何計算一個字符串中出現的次數?
- 11. 計算SQL中某個月發生的次數
- 12. 如何從文本文件中計算字符串中字符串的發生次數 - python
- 13. 計算字符串中每個字符的出現次數
- 14. Java計數從字符串中發生字符的次數是多少次
- 15. 計算髮生次數 - Excel
- 16. 使用計數器計算字符串空格的發生率
- 17. 如何計算在列表中相同的字符串發生了多少次
- 18. Java:如何計算非重複(只發生一次)ArrayList中的字符串?
- 19. 如何計算Dataframe字段中字符串的出現次數?
- 20. 如何計算字母出現在字符串中的次數?
- 21. 在JSON數組中計數字符串的發生次數
- 22. 如何計算字符串內的不同字符串vb.net
- 23. 按順序計算字符的發生次數
- 24. 在文本文件中計算字符的發生次數
- 25. 如何使用spring elasticsearch來計算字段發生的次數?
- 26. 如何計算字符串中子字符串的出現次數?
- 27. 如何計算列表中的字符串出現次數(字符串)
- 28. 如何計算一個月的事情發生的次數
- 29. 計算某個字符串在特定列中出現的次數
- 30. 如何計算字符串的字符?
你在哪裏搜索,試圖找到這個「計數」功能?哪個圖書館參考網站?哪個教程網站? – 2009-11-03 12:53:42