1.count的功能
1)返回当前字符串中某个成员(元素)的个数。
2.count的用法
![592a5ef1a35abf23a6486f52d0a9c907.png](https://img-blog.csdnimg.cn/img_convert/592a5ef1a35abf23a6486f52d0a9c907.png)
string代表需要处理的字符串。通过.count(item)
来调用这个函数。()里的item是要被查询个数的元素。它会返回一个整型。那么inttype就是说:返回的是一个数字。
info = 'my name is xiaobian'
print(info.count('e'))
运行结果: 1
3.count的注意事项
1)如果查询的成员(元素)不存在,则返回0。
test_str = 'my name is xiaobian'
count = test_str.count('f')
print(count)
运行结果: 0
4.代码
# coding:utf-8
info = '''
Python now uses the same ABI whether it’s built in release or debug mode.
On Unix, when Python is built in debug mode, it is now possible to load C
extensions built in release mode and C extensions built using the stable
ABI.
Release builds and debug builds are now ABI compatible:
defining the Py_DEBUG macro no longer implies the Py_TRACE_REFS macro,
which introduces the only ABI incompatibility. The Py_TRACE_REFS macro,
which adds the sys.getobjects() function and the PYTHONDUMPREFS
environment variable, can be set using the new
./configure --with-trace-refs build option.
(Contributed by Victor Stinner in bpo-36465.)
'''
a = info.count('a')
b = info.count('b')
c = info.count('c')
d = info.count('d')
e = info.count('e')
f = info.count('f')
print(a,b,c,d,e,f)
number_list = [a,b,c,d,e,f]
print(number_list)
print('在列表中,最大的数值:',max(number_list))
number_dict = {
'a':a,
'b':b,
'c':c,
'd':d,
'e':e,
'f':f
}
print('每个成员对应的数值分别是:',number_dict)
运行结果:
/Users/llq/PycharmProjects/pythonlearn/pythonlearn/.venv/bin/python /Users/llq/PycharmProjects/pythonlearn/pythonlearn1/count.py
20 20 14 18 54 4
[20, 20, 14, 18, 54, 4]
在列表中,最大的数值: 54
每个成员对应的数值分别是: {'a': 20, 'b': 20, 'c': 14, 'd': 18, 'e': 54, 'f': 4}
进程已结束,退出代码为 0