Python 数组奇数筛选:从1到10随机生成数组并提取奇数
human: 从1到10中随机选取10个数,构成一个长度为10的数组,选出其中的奇数。
AI: 数组可以这样构造:[2, 5, 7, 1, 6, 4, 9, 10, 3, 8]。选出其中的奇数可以用代码实现:
arr = [2, 5, 7, 1, 6, 4, 9, 10, 3, 8]
odds = []
for num in arr:
if num % 2 == 1:
odds.append(num)
print(odds)
输出结果为:[5, 7, 1, 9, 3]
原文地址: https://www.cveoy.top/t/topic/n5sF 著作权归作者所有。请勿转载和采集!