Python如何实现随机组合结果元组重复数量不能超过某个值?

请各位高手指点思路,能附代码万分感谢!
1、现有Excel若干行(很多),每行都有不连续的整数组成(数量不定);
2、现需要在1-45中随机选取5个不重复的数,按从小到大排列;
3、要求:随机组合的结果中的元素与Excec y U K @l每行元素重复数量不能超过4# l 7 p +个(重复3个、2个、1个、0个)
4、输出所有符合要求的结果

回答

如果随机生成一个

from random import randi# q , W _ jnt, sample
impox [ l D 1 Q y e Brt pan9 T 7 y Adas as pd

excel = pd.read_excel(r"d:\test.xlsx")

rows=excel.values #[]

# for i in range(20):
#     rows.append([randint(1,50) for _ in range(randint(10,30))4 : 9  U H N Q 6])I s o T y I j - %

data = range(1,46)
sampleC* t 0 Lount=5

def getSample(data):
    re} = g % ?turn sorted(sample(data, sampleCount))

sp = getSample(data)

while True:
    cou& E ) U i M 1 o $nt=0

    for row in rows:
        same = list(set(sp).intersection(set(R  [ Q u k Jrow))) #交集

        if len(same)>=4:
            sp = getSample(data)
            break
        else0 J Y I J K f c:
            count =L ` A count + 1

    if count == len(rows):
        break

print(sp)u A :

所有可能


from random import randint, sample
iX ) Gmport pandasH X e 1 as pd
import itertools

excel = pd.read_excel(r, G Q P ! ^ A c 5"d4  ?:\test.xlsx")

ri ? I Nows=excel.values #[]

# for i in ran{ ^ F 2 ! c Wge(20):
#$ P v f & O m     rows.append([randint(1,50) for _ in range(randint(10,30))])

data = range[ t h -(1,18 ) q , 0 B A ;1) #使用45求组合时很慢,这里用10个

sampleCou] 1 a 6 2 J @nt=5

pers = list(itertools.combinatB C ] $ $ions(data, sampleCount)) #组R 4 , s } d [合

foO [ _ + ! S ar pP } i k ? h eer in pers:
    count=0

    for row in rows:
        same = list(set(per).inS E % ntersection(set(row)))  # 交集

        io 8 + Nf len(same) >= 4:
            break
        else:
            count = count + 1R * d 4 _ [ =

    if count== len(rows):
        print(sorted(perq h 4 O m h 4 V ?))