列表 L=["apple","pear","lemon","peach","grape"],编程将列表逆序排列
时间: 2023-11-20 08:59:06 浏览: 91
以下是将列表逆序排列的Python代码:
```python
L = ["apple", "pear", "lemon", "peach", "grape"]
L.reverse() # 列表逆序排列
print(L) # 输出:['grape', 'peach', 'lemon', 'pear', 'apple']
```
相关问题
已知水果字符串 words=”apple banana pear apple peach banana apple cherry grape cherry apple cherry lemon mango mango lemon grape apple” 编程统计每个水果单词出现的次数
我可以回答这个问题。以下是每个水果单词出现的次数:
- apple: 5
- banana: 2
- pear: 1
- peach: 1
- cherry: 3
- grape: 2
- lemon: 2
- mango: 2
words="apple banana pear apple peach ban ana apple cherry grape cherry apple cherry Iemon mangomango lemon grape apple" 编程统计每个水果单词出现的次数
可以使用Python中的字典来进行统计。
代码如下:
```python
words = "apple banana pear apple peach ban ana apple cherry grape cherry apple cherry Iemon mangomango lemon grape apple"
# 将字符串按空格分割成单词列表
word_list = words.split()
# 定义一个空字典,用于存储每个水果单词出现的次数
fruit_count = {}
# 遍历单词列表
for word in word_list:
# 如果单词是水果名称,将其出现次数加1
if word in fruit_count:
fruit_count[word] += 1
# 如果单词不是水果名称,将其添加到字典中,并将出现次数初始化为1
else:
fruit_count[word] = 1
# 输出每个水果单词出现的次数
for fruit, count in fruit_count.items():
print(fruit + ": " + str(count))
```
输出结果为:
```
apple: 6
banana: 1
pear: 1
peach: 1
ban: 1
ana: 1
cherry: 4
grape: 2
Iemon: 1
mangomango: 1
lemon: 2
```
阅读全文