# 示例代码:使用 count 方法统计字符串或列表中某个元素出现的次数
# 对于字符串
string = "hello world"
count_result = string.count("l")
print(f"'l' 在字符串 '{string}' 中出现了 {count_result} 次")
# 对于列表
list_example = ['apple', 'banana', 'apple', 'orange', 'apple']
count_result_list = list_example.count('apple')
print(f"'apple' 在列表中出现了 {count_result_list} 次")
count
方法用于统计字符串或列表中某个指定元素出现的次数。count
方法会返回子字符串在原字符串中出现的次数。count
方法会返回指定元素在列表中出现的次数。上一篇:python回文数
下一篇:python list转str
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站