my_list = ["hello", "python", "hello", "itcast", "hello"],怎么使用遍历得出hello的数量
时间: 2023-05-18 10:03:55 浏览: 71
可以使用循环遍历列表中的元素,统计出现次数,代码如下:
```
count = 0
for item in my_list:
if item == "hello":
count += 1
print(count)
```
输出结果为:3
相关问题
my_list = ["hello", "python", "hello", "itcast", "hello"],怎么用count()查hello的重复值
你可以使用以下代码来查找 "hello" 在 my_list 中的重复次数:
```
my_list = ["hello", "python", "hello", "itcast", "hello"]
count = my_list.count("hello")
print(count)
```
输出结果为:
```
3
```
string_example='000itcast and itheima000 #字符串左右各两个空格index =string_example.find("it") nes_string_example=string_example.replace("000",”")#替换为一个空格nes_string1 =nes_string_example.strip0 print (index) print (nes_string_example) print (nes_string1)
这段代码有一些错误,可能是因为复制粘贴时出现了一些格式问题。我来帮你修正一下:
```
string_example = '000itcast and itheima000 #字符串左右各两个空格'
index = string_example.find("it")
new_string_example = string_example.replace("000", " ")
new_string1 = new_string_example.strip()
print(index)
print(new_string_example)
print(new_string1)
```
这段代码主要是对字符串 `string_example` 进行了一些操作:
1. 使用 `find` 函数找到子字符串 "it" 的索引位置,并将结果赋值给 `index`。
2. 使用 `replace` 函数将字符串中的 "000" 替换成一个空格,并将结果赋值给 `new_string_example`。
3. 使用 `strip` 函数去除字符串左右两端的空格,并将结果赋值给 `new_string1`。
4. 最后打印出 `index`、`new_string_example` 和 `new_string1`。
注意,这段代码中的变量名需要使用下划线 `_` 进行分隔,而不是驼峰式命名。另外,字符串需要用单引号或双引号括起来。