Generate all combinations with maximum difference of consecutive elements

浪尽此生 提交于 2019-12-11 15:51:44

问题


I would like to generate combinations with length 9 out of a sorted list (length 150) without any repeated values:

24, 110, 157, 256, 278, 368, 416, 502, 593, 666, 751, 801, 827, 925, 991, 1044, 1118, 1159, 1203, 1296, 1375, 1479, 1546, 1621, 1679, 1695, 1726, 1832, 1922, 1978, 1995, 2041, 2119, 2160, 2240, 2315, 2421, 2493, 2527, 2543, 2559, 2654, 2762, 2813, 2883, 2950, 2982, 3009, 3052, 3103, 3162, 3239, 3346, 3427, 3462, 3480, 3548, 3638, 3670, 3761, 3833, 3898, 3946, 3979, 4051, 4096, 4124, 4219, 4289, 4393, 4455, 4497, 4590, 4695, 4769, 4835, 4874, 4891, 4965, 5065, 5159, 5247, 5323, 5352, 5406, 5493, 5534, 5581, 5687, 5796, 5881, 5982, 6049, 6110, 6200, 6266, 6297, 6312, 6392, 6452, 6501, 6599, 6651, 6734, 6758, 6799, 6859, 6912, 6942, 7049, 7149, 7258, 7296, 7317, 7363, 7446, 7494, 7589, 7653, 7737, 7803, 7820, 7929, 8012, 8117, 8135, 8174, 8242, 8349, 8417, 8511, 8589, 8611, 8643, 8717, 8765, 8795, 8854, 8880, 8936, 8987, 9041, 9100, 9186, 9292, 9398, 9472, 9502, 9598, 9642, 9731, 9789, 9864, 9936

I already tried it with:

itertools.combinations(list, 9)

However, that's not very efficient and would generate more than a billion combinations. I only want the combinations where the difference between each pair of consecutive numbers is 150 or less. For example, the combination

(24, 110, 157, 256, 278, 368, 416, 502, 593)

has no consecutive pairs with a difference greater than 150, but the combination

(24, 110, 157, 256, 278, 368, 416, 502, 9936)

has a consecutive pair 502, 9936 with a difference greater than 150.

How can I achieve this in Python? The input list is sorted and I need the output to be sorted as well.


I already found this solution, which was very good. However the output wasn't sorted which was my problem, and it generates combinations where every pair has a maximum difference of 150 - not just consecutive pairs - so it excludes the example given above because 593 - 24 > 150.

import itertools
import random

def combs(nums):
    result = set()
    for lower in nums:
        options = [n for n in nums if lower <= n <= lower + 150]
        result.update(itertools.combinations(options, 9))
    return result

print(combs([random.randrange(0, 5000) for _ in range(150)]))

回答1:


To sort the output you can use python's sorted() function.

Full code:

import itertools, random

def combs(nums):
  result = set()
  for lower in nums:
     options = [n for n in nums if lower <= n <= lower + 150] 
     result.update(itertools.combinations(options, 9))
  return result

unsorted = combs([random.randrange(0, 5000) for _ in range(150)])

# Go through each element in your set and sort it - must convert result to tuple
sorted_set = set()
for elem in unsorted:
  elem = tuple(sorted(elem))
  sorted_set.add(elem)


来源:https://stackoverflow.com/questions/59020768/generate-all-combinations-with-maximum-difference-of-consecutive-elements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!