# 示例代码:使用 count 函数统计字符串中某个字符出现的次数
# 定义一个字符串
string = "hello world"
# 使用 count 函数统计字符串中 'l' 出现的次数
count_l = string.count('l')
# 输出结果
print(f"'l' 在字符串中出现了 {count_l} 次")
# 示例代码:使用 count 函数统计列表中某个元素出现的次数
# 定义一个列表
list_items = [1, 2, 3, 2, 4, 2, 5]
# 使用 count 函数统计列表中数字 2 出现的次数
count_2 = list_items.count(2)
# 输出结果
print(f"数字 2 在列表中出现了 {count_2} 次")
count
函数可以用于字符串和列表,用于统计某个特定元素或字符在序列中出现的次数。count
函数统计的是指定字符或子字符串在字符串中出现的次数。count
函数统计的是指定元素在列表中出现的次数。上一篇:python wxpython
下一篇:python request库
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站