笛卡尔乘积 - YamaDe
- 中文名
- 笛卡尔乘积
- 外文名
- Cartesian product
- 别 称
- 直积
- 表达式
- A×B = {(x,y)|x∈A∧y∈B}
- 提出者
- 笛卡尔
- 应用学科
- 数学
- 适用领域范围
- 运算
C#源代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Linq;
public class Descartes
{ public static void run(List<List< string >> dimvalue, List< string > result, int layer, string curstring)
{ if (layer < dimvalue.Count - 1)
{ if (dimvalue[layer].Count == 0)
run(dimvalue, result, layer + 1, curstring); else { for ( int i = 0; i < dimvalue[layer].Count; i++)
{ StringBuilder s1 = new StringBuilder();
s1.Append(curstring); s1.Append(dimvalue[layer][i]); run(dimvalue, result, layer + 1, s1.ToString()); } } } else if (layer == dimvalue.Count - 1)
{ if (dimvalue[layer].Count == 0) result.Add(curstring);
else { for ( int i = 0; i < dimvalue[layer].Count; i++)
{ result.Add(curstring + dimvalue[layer][i]); } } } } } |
使用说明
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
import java.util.ArrayList;
import java.util.List;
//import com.alibaba.fastjson.JSON; public class DescartesUtil {
public static void main(String[] args) {
List<List<String>> list = new ArrayList<List<String>>();
List<String> listSub1 = new ArrayList<String>();
List<String> listSub2 = new ArrayList<String>();
List<String> listSub3 = new ArrayList<String>();
List<String> listSub4 = new ArrayList<String>();
listSub1.add( "1" );
listSub1.add( "2" );
listSub2.add( "3" );
listSub2.add( "4" );
listSub3.add( "a" );
listSub3.add( "b" );
listSub4.add( "c" );
listSub4.add( "d" );
list.add(listSub1);
list.add(listSub2);
list.add(listSub3);
list.add(listSub4);
List<List<String>> result = new ArrayList<List<String>>();
descartes(list, result, 0 , new ArrayList<String>());
// System.out.println(JSON.toJSONString(result));
}
/**
* Created on 2014年4月27日
* <p>
* Discription:笛卡尔乘积算法
* 把一个List{[1,2],[3,4],[a,b]}转化成List{[1,3,a],[1,3,b],[1,4
* ,a],[1,4,b],[2,3,a],[2,3,b],[2,4,a],[2,4,b]}数组输出
* </p>
*
* @param dimvalue原List
* @param result通过乘积转化后的数组
* @param layer
* 中间参数
* @param curList
* 中间参数
*/
private static void descartes(List<List<String>> dimvalue,
List<List<String>> result, int layer, List<String> curList) {
if (layer < dimvalue.size() - 1 ) {
if (dimvalue.get(layer).size() == 0 ) {
DescartesUtil.descartes(dimvalue, result, layer + 1 , curList);
} else {
for ( int i = 0 ; i < dimvalue.get(layer).size(); i++) {
List<String> list = new ArrayList<String>(curList);
list.add(dimvalue.get(layer).get(i));
DescartesUtil.descartes(dimvalue, result, layer + 1 , list);
}
}
} else if (layer == dimvalue.size() - 1 ) {
if (dimvalue.get(layer).size() == 0 ) {
result.add(curList);
} else {
for ( int i = 0 ; i < dimvalue.get(layer).size(); i++) {
List<String> list = new ArrayList<String>(curList);
list.add(dimvalue.get(layer).get(i));
result.add(list);
}
}
}
}
} |
python源代码
1
2
3
4
|
from itertools import product
for x,y,z in product([ \'a\' , \'b\' , \'c\' ],[ \'d\' , \'e\' , \'f\' ],[ \'m\' , \'n\' ]):
# python大法好
print (x,y,z)
|
- 参考资料
-
- 1. 黄宏图,毕笃彦,查宇飞,高山,覃兵. 基于笛卡尔乘积字典的稀疏编码跟踪算法[J]. 电子与信息学报,2015,37(03):516-521. [2017-08-26].
- 2. JAVA笛卡尔(descartes)乘积运算结果的输出 .PHP爱好者.2014-05-07[引用日期2015-03-26]
- 3. 黄海圆. 笛卡尔乘积图的配对控制数[D].浙江师范大学,2015.