如何获取Python中list的子集

Python017

如何获取Python中list的子集,第1张

使用 itertools

import itertools

 

# 有序

print list(itertools.permutations([1,2,3,4],2))

[(1, 2), (1, 3), (1, 4), (2, 1), (2, 3), (2, 4), (3, 1), (3, 2), (3, 4), (4, 1), (4, 2), (4, 3)]

 

无序

print list(itertools.combinations([1,2,3,4],2))

[(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]

代码如下,仅供参考:

from  random import  randint

amount = int(input("随机数数量:"))

result = [randint(1,100) for i in range(amount)]

print(result)

输入:10

输出:[2, 65, 58, 24, 15, 12, 38, 24, 18, 79]