0

我有以下挑战,我需要从对象列表中创建唯一的对象元组。这里的特殊挑战是我如何为动态列表大小(n 维)做到这一点?如果您有固定尺寸,这很容易。我希望有人知道第三方 API,或者对我有一些提示,我可以如何实现这一点。

下面的示例显示了 3 个列表,因此更容易解释。我将对象按其类排序在列表中,例如

lista = {a1,a2}
listb = {b1,b2,b3}
listc = {c1,c2}

我喜欢有以下元组,以便每个对象彼此配对一次:

tuple1  = {a1,b1,c1}
tuple2  = {a1,b1,c2}
tuple3  = {a1,b2,c1}
tuple4  = {a1,b2,c2}
tuple5  = {a1,b3,c1}
tuple6  = {a1,b3,c2}
tuple7  = {a2,b1,c1}
tuple8  = {a2,b1,c2}
tuple9  = {a2,b2,c1}
tuple10 = {a2,b2,c2}
tuple11 = {a2,b3,c1}
tuple12 = {a2,b3,c2}


如果列表的数量是动态
的-列表的大小是动态的,我该如何实现这一点

任何提示或想法?先感谢您

4

3 回答 3

2

我们可以按如下方式构建解决方案。首先请注意,列表大小不同的情况在现代语言中从来都不是问题,因为今天几乎所有语言都支持可变大小的列表。更棘手的部分是列表数量不同时。

这是固定数量列表的 Python 案例:

lista = ["a1","a2"]
listb = ["b1","b2","b3"]
listc = ["c1","c2"]
[(a,b,c) for a in lista for b in listb for c in listc]

对于 Java,做同样的事情。您将需要三个嵌套的 for 循环:

List<String> lista = Arrays.asList("a1","a2");
List<String> listb = Arrays.asList("b1","b2","b3");
List<String> listc = Arrays.asList("c1","c2");

List<List<String>> result = new ArrayList<List<String>>();
for (String a: lista) {
    for (String b: listb) {
        for (String c: listc) {
            result.add(Arrays.asList(a, b, c));
        }
    }
}
System.out.println(result);

这产生

[[a1, b1, c1], [a1, b1, c2], [a1, b2, c1], [a1, b2, c2], [a1, b3, c1], [a1, b3, c2], [a2, b1, c1], [a2, b1, c2], [a2, b2, c1], [a2, b2, c2], [a2, b3, c1], [a2, b3, c2]]

现在的诀窍是如何执行不确定数量的 for 循环?一种方法是使用递归。

从 0 个列表(甚至 1 个)的基本情况开始,然后问自己:当我添加一个新列表时,结果如何变化?然后你可以组合一个很好的小递归公式。您需要这方面的帮助吗?

(顺便说一句:在 Python 中,我们有itertools这些东西。我不知道有任何 Java 类似物,但一些谷歌搜索可能会发现一些东西。)

好的,让我们为无限数量的列表开发递归公式。如果您只有一个列表,请说

a = ["a1", "a2"]

那么结果是[["a1"],["a2"]]。但现在让我们添加另一个列表:

a = ["b1", "b2"]

现在的答案是什么?

[[a1, b1], [a1, b2], [a2, b1], [a2, b2]]

我们是怎么得到的?答案是我们获取新列表的每个元素并将其附加到结果中的每个元素。每次我们添加一个新列表时,我们都会这样做。假设现在我们添加["c1","c2","c3"]. 你看到我们会得到什么吗?如果是这样,您现在应该能够定义递归步骤!

完整代码

好吧,我无法抗拒!干得好。

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * An application requested by a SO user.
 */
public class ProductExample {

    /**
     * Returns a list containing all possible products of a list of lists of strings.
     */
    public static List<List<String>> product(List<List<String>> lists) {
        List<List<String>> result = new ArrayList<List<String>>();

        if (lists.isEmpty()) {
            result.add(new ArrayList<String>());
            return result;
        }

        List<List<String>> partial = product(lists.subList(0, lists.size() - 1));
        for (List<String> list: partial) {
            for (String s: lists.get(lists.size() - 1)) {
                List<String> listCopy = new ArrayList<String>(list);
                listCopy.add(s);
                result.add(listCopy);
            }
        }
        return result;
    }


    public static void main(String[] args) {
        System.out.println(product(
                Arrays.asList(
                        Arrays.asList("a1", "a2"),
                        Arrays.asList("b1", "b2", "b3"),
                        Arrays.asList("c1", "c2"),
                        Arrays.asList("d1", "d2"))));
    }
}

Rant:这在 Python 中要容易得多。或者红宝石。:)

于 2011-08-11T08:06:28.853 回答
1

像这样的东西应该可以工作(视为伪代码):

List<List<int>> result = new List<List<int>>();
int[] indices = new int[number_of_lists];
for(int i = 0; i < indices.size(); i++)
  indices[i] = 0;

while(indices[0] < lists[0].size())
{
  List<int> temp = new List<int>();
  for(int i = 0; i < indices.size(); i++)
    temp.add(lists[i].get(indices[i]));
  result.add(temp);

  indices[indices.size() - 1] += 1;
  int i = indices.size();
  while(i >= 1 && indices[i] == lists[i].size())
  {
    indices[i] = 0;
    indices[i-1] += 1;
    i--;
  }
}
于 2011-08-11T08:14:05.513 回答
0

Sets of Google Guava似乎做了一些类似于你想要的事情,至少对于你的例子来说。但是,您需要将结果列表转换为元组形式。这些也是集合,而不是列表,但您的示例至少没有重复条目,所以也许这对您有所帮助。

于 2011-08-11T10:47:35.650 回答