Python 随机生成数组并提取奇数
以下是使用 Python 代码随机生成从 1 到 10 的 10 个数并提取其中的奇数的示例:
import random
# 生成从 1 到 10 的 10 个随机数
nums = [random.randint(1, 10) for i in range(10)]
print('生成的随机数为:', nums)
# 选出其中的奇数
odds = [num for num in nums if num % 2 == 1]
print('选出的奇数为:', odds)
运行结果可能如下:
生成的随机数为: [2, 8, 1, 9, 5, 6, 4, 1, 10, 1]
选出的奇数为: [1, 9, 5, 1, 1]
原文地址: https://www.cveoy.top/t/topic/n5sL 著作权归作者所有。请勿转载和采集!