创建两个列表的组合,直到一个列表中的值大于另一个列表中的值?

时间:2019-02-14 17:33:57

标签: python itertools

我设法对两个lists进行了所有可能的组合,但是直到list1中的值超过另一个为止,我一直不知道如何创建组合。

 list1 = [10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,90,100]

 list2 = [30,35,40,45,50,55,60,65,70,75,80,85,90,95,100,105,110,115,120,125,130,135,
          140,150,155,160,165,170,
          175,180,185,195,200]

我尝试过并且仍在修改的代码,在此处搜索了stackoverflow:combination of two lists

 y = [zip(x,list1) for x in itertools.permutations(list2,len(list1)) if x > list1]

预期输出:

combi = [[(30,10),(30,15),(30,20),(30,25)],[(35,10),......,
          (35,30)],.....,[(120,10),.......,(120,100)......]]

如果有一种方法可以很好地限制组合起来的第一项和第二项之间的差异。

谢谢

2 个答案:

答案 0 :(得分:2)

您无需为此使用itertoolszip。您可以通过列表理解中的两个for循环来实现:

result = [[(elem2,elem1) for elem1 in list1 if elem1 < elem2] for elem2 in list2]

答案 1 :(得分:1)

您可以使用嵌套列表理解:

[[(a, b) for b in list1 if b < a] for a in list2]

这将返回:

[[(30, 10), (30, 15), (30, 20), (30, 25)], [(35, 10), (35, 15), (35, 20), (35, 25), (35, 30)], [(40, 10), (40, 15), (40, 20), (40, 25), (40, 30), (40, 35)], [(45, 10), (45, 15), (45, 20), (45, 25), (45, 30), (45, 35), (45, 40)], [(50, 10), (50, 15), (50, 20), (50, 25), (50, 30), (50, 35), (50, 40), (50, 45)], [(55, 10), (55, 15), (55, 20), (55, 25), (55, 30), (55, 35), (55, 40), (55, 45), (55, 50)], [(60, 10), (60, 15), (60, 20), (60, 25), (60, 30), (60, 35), (60, 40), (60, 45), (60, 50), (60, 55)], [(65, 10), (65, 15), (65, 20), (65, 25), (65, 30), (65, 35), (65, 40), (65, 45), (65, 50), (65, 55), (65, 60)], [(70, 10), (70, 15), (70, 20), (70, 25), (70, 30), (70, 35), (70, 40), (70, 45), (70, 50), (70, 55), (70, 60), (70, 65)], [(75, 10), (75, 15), (75, 20), (75, 25), (75, 30), (75, 35), (75, 40), (75, 45), (75, 50), (75, 55), (75, 60), (75, 65), (75, 70)], [(80, 10), (80, 15), (80, 20), (80, 25), (80, 30), (80, 35), (80, 40), (80, 45), (80, 50), (80, 55), (80, 60), (80, 65), (80, 70), (80, 75)], [(85, 10), (85, 15), (85, 20), (85, 25), (85, 30), (85, 35), (85, 40), (85, 45), (85, 50), (85, 55), (85, 60), (85, 65), (85, 70), (85, 75), (85, 80)], [(90, 10), (90, 15), (90, 20), (90, 25), (90, 30), (90, 35), (90, 40), (90, 45), (90, 50), (90, 55), (90, 60), (90, 65), (90, 70), (90, 75), (90, 80)], [(95, 10), (95, 15), (95, 20), (95, 25), (95, 30), (95, 35), (95, 40), (95, 45), (95, 50), (95, 55), (95, 60), (95, 65), (95, 70), (95, 75), (95, 80), (95, 90)], [(100, 10), (100, 15), (100, 20), (100, 25), (100, 30), (100, 35), (100, 40), (100, 45), (100, 50), (100, 55), (100, 60), (100, 65), (100, 70), (100, 75), (100, 80), (100, 90)], [(105, 10), (105, 15), (105, 20), (105, 25), (105, 30), (105, 35), (105, 40), (105, 45), (105, 50), (105, 55), (105, 60), (105, 65), (105, 70), (105, 75), (105, 80), (105, 90), (105, 100)], [(110, 10), (110, 15), (110, 20), (110, 25), (110, 30), (110, 35), (110, 40), (110, 45), (110, 50), (110, 55), (110, 60), (110, 65), (110, 70), (110, 75), (110, 80), (110, 90), (110, 100)], [(115, 10), (115, 15), (115, 20), (115, 25), (115, 30), (115, 35), (115, 40), (115, 45), (115, 50), (115, 55), (115, 60), (115, 65), (115, 70), (115, 75), (115, 80), (115, 90), (115, 100)], [(120, 10), (120, 15), (120, 20), (120, 25), (120, 30), (120, 35), (120, 40), (120, 45), (120, 50), (120, 55), (120, 60), (120, 65), (120, 70), (120, 75), (120, 80), (120, 90), (120, 100)], [(125, 10), (125, 15), (125, 20), (125, 25), (125, 30), (125, 35), (125, 40), (125, 45), (125, 50), (125, 55), (125, 60), (125, 65), (125, 70), (125, 75), (125, 80), (125, 90), (125, 100)], [(130, 10), (130, 15), (130, 20), (130, 25), (130, 30), (130, 35), (130, 40), (130, 45), (130, 50), (130, 55), (130, 60), (130, 65), (130, 70), (130, 75), (130, 80), (130, 90), (130, 100)], [(135, 10), (135, 15), (135, 20), (135, 25), (135, 30), (135, 35), (135, 40), (135, 45), (135, 50), (135, 55), (135, 60), (135, 65), (135, 70), (135, 75), (135, 80), (135, 90), (135, 100)], [(140, 10), (140, 15), (140, 20), (140, 25), (140, 30), (140, 35), (140, 40), (140, 45), (140, 50), (140, 55), (140, 60), (140, 65), (140, 70), (140, 75), (140, 80), (140, 90), (140, 100)], [(150, 10), (150, 15), (150, 20), (150, 25), (150, 30), (150, 35), (150, 40), (150, 45), (150, 50), (150, 55), (150, 60), (150, 65), (150, 70), (150, 75), (150, 80), (150, 90), (150, 100)], [(155, 10), (155, 15), (155, 20), (155, 25), (155, 30), (155, 35), (155, 40), (155, 45), (155, 50), (155, 55), (155, 60), (155, 65), (155, 70), (155, 75), (155, 80), (155, 90), (155, 100)], [(160, 10), (160, 15), (160, 20), (160, 25), (160, 30), (160, 35), (160, 40), (160, 45), (160, 50), (160, 55), (160, 60), (160, 65), (160, 70), (160, 75), (160, 80), (160, 90), (160, 100)], [(165, 10), (165, 15), (165, 20), (165, 25), (165, 30), (165, 35), (165, 40), (165, 45), (165, 50), (165, 55), (165, 60), (165, 65), (165, 70), (165, 75), (165, 80), (165, 90), (165, 100)], [(170, 10), (170, 15), (170, 20), (170, 25), (170, 30), (170, 35), (170, 40), (170, 45), (170, 50), (170, 55), (170, 60), (170, 65), (170, 70), (170, 75), (170, 80), (170, 90), (170, 100)], [(175, 10), (175, 15), (175, 20), (175, 25), (175, 30), (175, 35), (175, 40), (175, 45), (175, 50), (175, 55), (175, 60), (175, 65), (175, 70), (175, 75), (175, 80), (175, 90), (175, 100)], [(180, 10), (180, 15), (180, 20), (180, 25), (180, 30), (180, 35), (180, 40), (180, 45), (180, 50), (180, 55), (180, 60), (180, 65), (180, 70), (180, 75), (180, 80), (180, 90), (180, 100)], [(185, 10), (185, 15), (185, 20), (185, 25), (185, 30), (185, 35), (185, 40), (185, 45), (185, 50), (185, 55), (185, 60), (185, 65), (185, 70), (185, 75), (185, 80), (185, 90), (185, 100)], [(195, 10), (195, 15), (195, 20), (195, 25), (195, 30), (195, 35), (195, 40), (195, 45), (195, 50), (195, 55), (195, 60), (195, 65), (195, 70), (195, 75), (195, 80), (195, 90), (195, 100)], [(200, 10), (200, 15), (200, 20), (200, 25), (200, 30), (200, 35), (200, 40), (200, 45), (200, 50), (200, 55), (200, 60), (200, 65), (200, 70), (200, 75), (200, 80), (200, 90), (200, 100)]]