从子列表中检索升序整数的所有可能组合
Retrieve all possible combinations of ascending integers from sublists
我有包含子列表的列表。从这些列表中,我想检索所有按升序排列的整数组合。子列表的顺序也很重要(参见预期输出)。
当函数也 return 整数本身时,这并不是一件坏事(请参阅预期输出中的可选子列表)。
此外,当一个子列表有多个值时,我也想将它们视为单独的组合。这些值不能同时出现(参见示例 3)。
example_list = [[1], [0], [4], [2]]
get_ascending_sublist_values(example_list)
>> [[1, 4], [1, 2], [0, 4], [0, 2] (optional: [1], [0], [4], [2])]
example_list2 = [[1], [0], [4], [2], [5]]
get_ascending_sublist_values(example_list2)
>> [[1, 4, 5], [1, 2, 5], [0, 4, 5], [0, 2, 5], [1, 4], [1, 2], [0, 4], [0, 2], [0, 5], [(optional: [1], [0], [4], [2], [5])]
example_list3 = [[0], [1, 4], [2]]
get_ascending_sublist_values(example_list3)
>> [[0, 1, 2], [0, 1], [0, 4], [0, 2], [1, 2], (optional: [1], [0], [4], [2])]
使用 itertools.combinations
和 itertools.product
。这不是一个有效的解决方案,因为这不是必需的。使其更有效(即使用回溯)将需要相当多的工作,并且理论上它仍然不能低于 o(2^n)
.
from itertools import combinations
from itertools import product
def get_ascending_sublist_values(a):
filtered = set()
for comb_length in range(2, len(a)+1):
combs = combinations(a, comb_length)
results = []
for comb in combs:
for i in range(len(comb) - 1):
prods = product(*comb)
for prod in prods:
if sorted(prod) == list(prod):
results.append(tuple(sorted(prod)))
for r in results:
filtered.add(r)
print(filtered)
a1 = [[1], [0], [4], [2]]
a2 = [[1], [0], [4], [2], [5]]
a3 = [[0], [1, 4], [2]]
get_ascending_sublist_values(a1)
print("----------")
get_ascending_sublist_values(a2)
print("----------")
get_ascending_sublist_values(a3)
输出:
{(1, 2), (0, 2), (1, 4), (0, 4)}
----------
{(1, 2), (0, 4, 5), (4, 5), (1, 4), (1, 4, 5), (1, 5), (0, 5), (0, 2, 5), (0, 4), (2, 5), (1, 2, 5), (0, 2)}
----------
{(0, 1), (1, 2), (0, 1, 2), (0, 4), (0, 2)}
我有包含子列表的列表。从这些列表中,我想检索所有按升序排列的整数组合。子列表的顺序也很重要(参见预期输出)。
当函数也 return 整数本身时,这并不是一件坏事(请参阅预期输出中的可选子列表)。
此外,当一个子列表有多个值时,我也想将它们视为单独的组合。这些值不能同时出现(参见示例 3)。
example_list = [[1], [0], [4], [2]]
get_ascending_sublist_values(example_list)
>> [[1, 4], [1, 2], [0, 4], [0, 2] (optional: [1], [0], [4], [2])]
example_list2 = [[1], [0], [4], [2], [5]]
get_ascending_sublist_values(example_list2)
>> [[1, 4, 5], [1, 2, 5], [0, 4, 5], [0, 2, 5], [1, 4], [1, 2], [0, 4], [0, 2], [0, 5], [(optional: [1], [0], [4], [2], [5])]
example_list3 = [[0], [1, 4], [2]]
get_ascending_sublist_values(example_list3)
>> [[0, 1, 2], [0, 1], [0, 4], [0, 2], [1, 2], (optional: [1], [0], [4], [2])]
使用 itertools.combinations
和 itertools.product
。这不是一个有效的解决方案,因为这不是必需的。使其更有效(即使用回溯)将需要相当多的工作,并且理论上它仍然不能低于 o(2^n)
.
from itertools import combinations
from itertools import product
def get_ascending_sublist_values(a):
filtered = set()
for comb_length in range(2, len(a)+1):
combs = combinations(a, comb_length)
results = []
for comb in combs:
for i in range(len(comb) - 1):
prods = product(*comb)
for prod in prods:
if sorted(prod) == list(prod):
results.append(tuple(sorted(prod)))
for r in results:
filtered.add(r)
print(filtered)
a1 = [[1], [0], [4], [2]]
a2 = [[1], [0], [4], [2], [5]]
a3 = [[0], [1, 4], [2]]
get_ascending_sublist_values(a1)
print("----------")
get_ascending_sublist_values(a2)
print("----------")
get_ascending_sublist_values(a3)
输出:
{(1, 2), (0, 2), (1, 4), (0, 4)}
----------
{(1, 2), (0, 4, 5), (4, 5), (1, 4), (1, 4, 5), (1, 5), (0, 5), (0, 2, 5), (0, 4), (2, 5), (1, 2, 5), (0, 2)}
----------
{(0, 1), (1, 2), (0, 1, 2), (0, 4), (0, 2)}