用python实现随机生成三个有十个元素的数组把三个合并成一个数组挑出...
发布网友
发布时间:2022-04-23 04:00
我来回答
共1个回答
热心网友
时间:2022-04-07 09:47
#coding=utf-8
'''
Created on 2012-6-4
@author: Administrator
'''
import random
def test():
minNum = 0#随机数起始
maxNum = 999999#随机数最大
#随机生成三个序列,且为整数
list1 = [random.randint(minNum, maxNum) for i in xrange(10)]
list2 = [random.randint(minNum, maxNum) for i in xrange(10)]
list3 = [random.randint(minNum, maxNum) for i in xrange(10)]
lastList = list1 + list2 + list3#合在一起
lastList = list(set(lastList))#去掉重复,利用set是无序不重复的
oddNumList = []#用于保存奇数序列
evenNumList = []#用于保存偶数
for num in lastList:
if num % 2 == 0:
evenNumList.append(num)
else:
oddNumList.append(num)
print u"奇数有:", oddNumList, "共%d个" % len(oddNumList)
print u"偶数有:", evenNumList, "共%d个" % len(evenNumList)
if __name__ == '__main__':
test()