python输出[3,4,5,6,7,11,12,13,14,16,17,21,22,23,24,25,27,28,29]列表所有奇数及奇数的平均值
时间: 2023-09-29 15:06:11 浏览: 52
可以使用列表推导式和内置函数来实现:
```python
lst = [3,4,5,6,7,11,12,13,14,16,17,21,22,23,24,25,27,28,29]
# 获取所有奇数
odd_nums = [num for num in lst if num % 2 != 0]
# 计算奇数的平均值
odd_avg = sum(odd_nums) / len(odd_nums)
# 输出结果
print("所有奇数为:", odd_nums)
print("奇数的平均值为:", odd_avg)
```
输出结果为:
```
所有奇数为: [3, 5, 7, 11, 13, 17, 21, 23, 25, 27, 29]
奇数的平均值为: 15.454545454545455
```
阅读全文