[英]How to select all possible sub-list pairs from a given list whose union will be the list?
Given a list say [4,5,6,7] i want to write a function that will output all pairs like ([4,5,7],[6]) ,( [4,5] , [6,7]) length of sub-list are not fixed 给定一个列表,说[4,5,6,7]我想编写一个函数,该函数将输出所有对,例如([4,5,7],[6]),([4,5],[6,7 ])子列表的长度不固定
i used a for loop and two new lists to append ith element to list one and append the remaining to list 2 then repeat the process i know this is a silly approach 我使用了一个for循环和两个新列表,将第ith个元素附加到列表一个,并将其余元素附加到列表2,然后重复该过程,我知道这是一个愚蠢的方法
a = [4,5,6,7]
for x in range(0,len(a)):
b = []
c = []
b.append(a[x])
for y in range(0,len(a)):
if y!=x:
c.append(a[y])
print(b,c)
for x in range(0,len(a)-1):
b = []
c = []
b.append(a[x])
b.append(a[x+1])
for y in range(0,len(a)):
if y!=x and y!=x+1:
c.append(a[y])
print(b,c)
i expect to print all possible sub-lists but always miss the non contiguous ones , i get( [4, 5 ] , [6,7] ) but never generate ([4,6],[5,7]) 我希望打印所有可能的子列表,但始终会错过不连续的子列表,我会得到([4,5],[6,7]),但不会生成([4,6],[5,7])
You can use itertools module to construct all such sublists: 您可以使用itertools模块构造所有此类子列表:
import itertools as it
s = {4,5,6,7}
for length in range(len(s)):
for combination in it.combinations(s, length):
print(list(combination), list(s - set(combination)))
[] [4, 5, 6, 7] [4] [5, 6, 7] [5] [4, 6, 7] [6] [4, 5, 7] [7] [4, 5, 6] [4, 5] [6, 7] [4, 6] [5, 7] [4, 7] [5, 6] [5, 6] [4, 7] [5, 7] [4, 6] [6, 7] [4, 5] [4, 5, 6] [7] [4, 5, 7] [6] [4, 6, 7] [5] [5, 6, 7] [4]
Here is a solution that does not remove duplicates: 这是不删除重复项的解决方案:
from itertools import combinations
a = [4,4,5,6,7]
result = []
for x in range(len(a) + 1):
for left in combinations(a, x):
right = a.copy()
for ele in left:
right.remove(ele)
result.append([list(left), right])
for res in result:
print(res)
Output: 输出:
[[], [4, 4, 5, 6, 7]]
[[4], [4, 5, 6, 7]]
[[4], [4, 5, 6, 7]]
[[5], [4, 4, 6, 7]]
[[6], [4, 4, 5, 7]]
[[7], [4, 4, 5, 6]]
[[4, 4], [5, 6, 7]]
[[4, 5], [4, 6, 7]]
[[4, 6], [4, 5, 7]]
[[4, 7], [4, 5, 6]]
[[4, 5], [4, 6, 7]]
[[4, 6], [4, 5, 7]]
[[4, 7], [4, 5, 6]]
[[5, 6], [4, 4, 7]]
[[5, 7], [4, 4, 6]]
[[6, 7], [4, 4, 5]]
[[4, 4, 5], [6, 7]]
[[4, 4, 6], [5, 7]]
[[4, 4, 7], [5, 6]]
[[4, 5, 6], [4, 7]]
[[4, 5, 7], [4, 6]]
[[4, 6, 7], [4, 5]]
[[4, 5, 6], [4, 7]]
[[4, 5, 7], [4, 6]]
[[4, 6, 7], [4, 5]]
[[5, 6, 7], [4, 4]]
[[4, 4, 5, 6], [7]]
[[4, 4, 5, 7], [6]]
[[4, 4, 6, 7], [5]]
[[4, 5, 6, 7], [4]]
[[4, 5, 6, 7], [4]]
[[4, 4, 5, 6, 7], []]
You can make a random sublist, then get the elements on the second list that are not iun the random sublist: 您可以创建一个随机子列表,然后获取第二个列表中不属于该随机子列表的元素:
def get_from_0_to_len_minus_1_elements_from_original_list():
pass # You implement this
list1 = get_from_0_to_len_minus_1_elements_from_original_list()
list2 = [element for element in original_list if element not in list1]
print(list1, list2)
As to how you partition the original list into random chunks that is up to you to solve 关于如何将原始列表划分为随机块,具体取决于您解决
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.