我有一个可变大小的列表,例如
[1, 2, 3, 4]
我希望尽一切可能将此列表拆分为两个:
([], [1, 2, 3, 4])
([1], [2, 3, 4])
([2], [1, 3, 4])
([3], [1, 2, 4])
([4], [1, 2, 3])
([1, 2], [3, 4])
([1, 3], [2, 4])
([1, 4], [2, 3])
([2, 3], [1, 4])
([2, 4], [1, 3])
([3, 4], [1, 2])
([1, 2, 3], [4])
([1, 2, 4], [3])
([1, 3, 4], [2])
([2, 3, 4], [1])
([1, 2, 3, 4], [])
我很确定这不是一个未知的问题,可能有一个算法,但我找不到。此外,这不应该使用任何外部库,而是使用大多数语言中的简单语言功能(循环,条件,方法/函数,变量......)。
我在Python中编写了一个hackish解决方案:
def get_all(objects):
for i in range(1, len(objects)):
for a in combinations(objects, i):
for b in combinations([obj for obj in objects if obj not in up], len(objects) - i):
yield State(up, down)
if objects:
yield State([], objects)
yield State(objects, [])
但是,它使用库功能,一般看起来不太好。
答案 0 :(得分:5)
l = [1, 2, 3, 4]
flags = [False] * len(l)
while True:
a = [l[i] for i, flag in enumerate(flags) if flag]
b = [l[i] for i, flag in enumerate(flags) if not flag]
print a, b
for i in xrange(len(l)):
flags[i] = not flags[i]
if flags[i]:
break
else:
break
结果:
[] [1, 2, 3, 4]
[1] [2, 3, 4]
[2] [1, 3, 4]
[1, 2] [3, 4]
[3] [1, 2, 4]
[1, 3] [2, 4]
[2, 3] [1, 4]
[1, 2, 3] [4]
[4] [1, 2, 3]
[1, 4] [2, 3]
[2, 4] [1, 3]
[1, 2, 4] [3]
[3, 4] [1, 2]
[1, 3, 4] [2]
[2, 3, 4] [1]
[1, 2, 3, 4] []
它很容易适应java:
public static void main(String[] args) {
int[] l = new int[] { 1, 2, 3, 4 };
boolean[] flags = new boolean[l.length];
for (int i = 0; i != l.length;) {
ArrayList<Integer> a = new ArrayList<>(), b = new ArrayList<>();
for (int j = 0; j < l.length; j++)
if (flags[j]) a.add(l[j]); else b.add(l[j]);
System.out.println("" + a + ", " + b);
for (i = 0; i < l.length && !(flags[i] = !flags[i]); i++);
}
}
答案 1 :(得分:2)
使用按位算术计算应易于转换为Java的子集的更低级别的解决方案:
def sublists(xs):
l = len(xs)
for i in range(1 << l):
incl, excl = [], []
for j in range(l):
if i & (1 << j):
incl.append(xs[j])
else:
excl.append(xs[j])
yield (incl, excl)
答案 2 :(得分:2)
虽然在Python中,通过Java的扩展库很容易获得结果,但您可以编写递归解决方案。以下将打印阵列的所有可能组合:
public static void main(String[] args) {
List<Integer> num = Arrays.asList(1, 2, 3, 4);
List<List<Integer>> sublists = new ArrayList<List<Integer>>();
for (int i = 0; i <= num.size(); i++) {
permutation(num, sublists, i, new ArrayList<Integer>(), 0);
}
for (List<Integer> subList : sublists) {
List<Integer> numCopy = new ArrayList<Integer>(num);
numCopy.removeAll(subList);
System.out.println("(" + subList + ", " + numCopy + ")");
}
}
public static void permutation(List<Integer> nums, List<List<Integer>> subLists, int sublistSize, List<Integer> currentSubList,
int startIndex) {
if (sublistSize == 0) {
subLists.add(currentSubList);
} else {
sublistSize--;
for (int i = startIndex; i < nums.size(); i++) {
List<Integer> newSubList = new ArrayList<Integer>(currentSubList);
newSubList.add(nums.get(i));
permutation(nums, subLists, sublistSize, newSubList, i + 1);
}
}
}
sublists
包含到目前为止发现的所有组合。最后一个参数是当前子列表的下一个元素的startIndex
。那是为了避免重复。
答案 3 :(得分:1)
查看所有不同大小的组合并从原始列表中“减去”它们似乎是直观的方法IMO:
from itertools import combinations
s = [1, 2, 3, 4]
for combs in (combinations(s, r) for r in range(len(s)+1)) :
for comb in combs:
diff = list(set(s[:]) - set(comb))
print diff, list(comb)
<强>输出强>
[1, 2, 3, 4] []
[2, 3, 4] [1]
[1, 3, 4] [2]
[1, 2, 4] [3]
[1, 2, 3] [4]
[3, 4] [1, 2]
[2, 4] [1, 3]
[2, 3] [1, 4]
[1, 4] [2, 3]
[1, 3] [2, 4]
[1, 2] [3, 4]
[4] [1, 2, 3]
[3] [1, 2, 4]
[2] [1, 3, 4]
[1] [2, 3, 4]
[] [1, 2, 3, 4]
可以用Java应用相同的方法(只是它更详细......):
private static List<Integer> initial;
public static void main(String[] args) throws IOException {
initial = Arrays.asList(1, 2, 3);
combinations(initial);
}
static void combinations(List<Integer> src) {
combinations(new LinkedList<>(), src);
}
private static void combinations(LinkedList<Integer> prefix, List<Integer> src) {
if (src.size() > 0) {
prefix = new LinkedList<>(prefix); //create a copy to not modify the orig
src = new LinkedList<>(src); //copy
Integer curr = src.remove(0);
print(prefix, curr); // <-- this is the only thing that shouldn't appear in a "normal" combinations method, and which makes it print the list-pairs
combinations(prefix, src); // recurse without curr
prefix.add(curr);
combinations(prefix, src); // recurse with curr
}
}
// print the prefix+curr, as one list, and initial-(prefix+curr) as a second list
private static void print(LinkedList<Integer> prefix, Integer curr) {
prefix = new LinkedList<>(prefix); //copy
prefix.add(curr);
System.out.println(Arrays.toString(prefix.toArray()) +
" " + Arrays.toString(subtract(initial, prefix).toArray()));
}
private static List<Integer> subtract(List<Integer> initial, LinkedList<Integer> prefix) {
initial = new LinkedList<>(initial); //copy
initial.removeAll(prefix);
return initial;
}
<强>输出强>
[1] [2, 3]
[2] [1, 3]
[3] [1, 2]
[2, 3] [1]
[1, 2] [3]
[1, 3] [2]
[1, 2, 3] []