定义一个长度为5的字符串数组,并初始化其初值为“open”, “door”, “the”, “open”, “name”;计算该数组中 “open”出现的次数,并倒序输出数组元素
时间: 2023-10-19 19:07:27 浏览: 165
下面是对应的Python代码实现:
```python
arr = ["open", "door", "the", "open", "name"]
count = arr.count("open") # 计算 "open" 出现的次数
print("open 出现的次数为:", count)
arr.reverse() # 倒序输出数组元素
print("倒序输出数组元素为:", arr)
```
输出结果为:
```
open 出现的次数为: 2
倒序输出数组元素为: ['name', 'open', 'the', 'door', 'open']
```
阅读全文