我想从排序列表(长度150)中生成长度为9的组合,且没有任何重复值:
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,91 86,9292,9398,9472,9502,9598,9642,9731,9789,9864,9936
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
我已经尝试过:
itertoolsbinations(list, 9)但是,这不是很有效,会产生十亿以上的组合。我只希望每对连续数字之间的差为150或更小的组合。例如,组合
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)没有连续的对,相差大于150,但组合在一起
has no consecutive pairs with a difference greater than 150, but the combination
(24, 110, 157, 256, 278, 368, 416, 502, 9936)连续有一对 502,9936 大于150。
has a consecutive pair 502, 9936 with a difference greater than 150.
如何在Python中实现呢?输入列表已排序,我也需要对输出进行排序。
How can I achieve this in Python? The input list is sorted and I need the output to be sorted as well.
我已经找到了此解决方案,这是很好。但是输出没有排序,这是我的问题,它会生成组合,其中每对的最大差值是150-不仅是连续的对-因此它排除了上面给出的示例,因为593-24> 150。
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(itertoolsbinations(options, 9)) return result print(combs([random.randrange(0, 5000) for _ in range(150)]))推荐答案
要对输出进行排序,可以使用python的 sorted( )函数。
To sort the output you can use python's sorted() function.
完整代码:
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(itertoolsbinations(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)更多推荐
生成具有最大连续元素差的所有组合
发布评论