我有以下字符串数组:
var myArray1 = new string[] {"A", "B", "C"};
var myArray2 = new string[] {"t1", "t2"};
Run Code Online (Sandbox Code Playgroud)
我希望能够生成一个如下所示的最终字符串:
var myFinalString = "t1.A, t2.A, t1.B, t2.B, t1.C, t2.C";
Run Code Online (Sandbox Code Playgroud)
我知道我可以迭代遍历数组的每个元素并手动构建字符串.但是,我想知道是否有更好的方法.我试图找出如何使String.Join()方法工作,但不能:-(.
我有两个像这样的数据表:
DT1:
ID1
----------
1
2
3
4
5
6
7
8
9
10
Run Code Online (Sandbox Code Playgroud)
DT2:
ID2
----------
1
2
3
4
5
Run Code Online (Sandbox Code Playgroud)
现在,我想从这两个数据表中检索项目的所有组合,这样结果将包含50(10 x 5)行 - 如下所示:
dtResult:
ID1 ID2
------------
1 1
1 2
1 3
1 4
1 5
2 1
2 2
2 3
2 4
2 5
3 1
. .
. .
. .
Run Code Online (Sandbox Code Playgroud)
有没有简单的方法而不是使用循环?
http://spark-university.s3.amazonaws.com/berkeley-saas/homework/hw1.pdf
试图完成这项任务的第7部分.以下代码似乎不起作用,但坦率地说我不知道为什么,自动分级器会留下代码后面的注释.
class CartesianProduct
include Enumerable
def initialize(arr1 = [], arr2 = [])
@arr1 = arr1
@arr2 = arr2
end
def each
prod = []
@arr1.each do |i|
@arr2.each do |j|
prod << [i, j]
end
end
prod.each
end
end
On Time
CartesianProduct
Failures:
1) CartesianProduct should work for the first example given in the homework [15 points]
Failure/Error: c.to_a.should include([:a, 4],[:a,5],[:b,5],[:b,4])
expected [] to include [:a, 4], [:a, 5], [:b, 5], and [:b, 4]
Diff:
@@ -1,2 +1,2 @@ …Run Code Online (Sandbox Code Playgroud) 我试图在Perl中编写一个函数来计算两个字符串的交叉积(笛卡尔积).我在Python中有类似的代码,如下所示:
def cross(A, B):
"Cross product of elements in A and elements in B."
return [a+b for a in A for b in B]
Run Code Online (Sandbox Code Playgroud)
我怎么能以优雅的方式模仿这个列表理解?
这是我到目前为止:
# compute the cross product of two Strings
# cross('12','AB') = ((1,A), (1,B), (2,A), (2,B))
sub cross {
# unpack strings
my ($A, $B) = @_;
# array to hold products
my @out_array;
# split strings into arrays
my @A_array = split(//, $A);
my @B_array = split(//, $B);
# glue the characters together and …Run Code Online (Sandbox Code Playgroud) 我有一个列表列表(在erlang中,字符串是列表),如下所示:
[ "ABC", "DEF"]
我希望在函数返回的列表中获得以下组合:
["ad","ae","af","bd","be","bf","cd","ce","cf"]
是否可以使用列表理解?我事先不知道列表的维度
我一直在寻找关于如何提出可以应用笛卡尔积的代码段的数周时间。假设我有两个数组:
int M[2]= {1,2};
int J[3] = {0,1,2};
Run Code Online (Sandbox Code Playgroud)
所以代码将采用这两个数组应用规则 MXJ 因此我们将有 (1,0)(1,1)(1,2)(2,0)(2,1)(2,2) 和我希望将新结果保存到一个新数组中,其中数组中的每个索引都包含一个 pair ,例如 c[0] = (1,0)。请帮忙 :(
以下代码有效,但显示详细.
def gen(l):
for x in range(l[0]):
for y in range(l[1]):
for z in range(l[2]):
yield [x, y, z]
l = [1, 2, 3]
print(list(gen(l)))
>>>[[0, 0, 0], [0, 0, 1], [0, 0, 2], [0, 1, 0], [0, 1, 1], [0, 1, 2]]
Run Code Online (Sandbox Code Playgroud)
我的目的是用itertools.product减少LOC.这就是我想出来的.
from itertools import product
def gen(l):
for x, y, z in product(map(range, l)):
yield [x, y, z]
l = [1, 2, 3]
print(list(gen(l)))
ValueError: not enough values to unpack (expected 3, got 1)
Run Code Online (Sandbox Code Playgroud)
是否有不同的方法来使用itertools.product以便有足够的值来解压缩?
python generator cartesian-product python-itertools python-3.x
我必须比较坐标才能获得距离。因此,我用sc.textFile()加载数据并制成笛卡尔积。文本文件中大约有2.000.000行,因此需要比较2.000.000 x 2.000.000坐标。
我用大约2.000的坐标测试了代码,并且在几秒钟内运行良好。但是使用大文件似乎在某个时刻停止了,我不知道为什么。该代码如下所示:
def concat(x,y):
if(isinstance(y, list)&(isinstance(x,list))):
return x + y
if(isinstance(x,list)&isinstance(y,tuple)):
return x + [y]
if(isinstance(x,tuple)&isinstance(y,list)):
return [x] + y
else: return [x,y]
def haversian_dist(tuple):
lat1 = float(tuple[0][0])
lat2 = float(tuple[1][0])
lon1 = float(tuple[0][2])
lon2 = float(tuple[1][2])
p = 0.017453292519943295
a = 0.5 - cos((lat2 - lat1) * p)/2 + cos(lat1 * p) * cos(lat2 * p) * (1 - cos((lon2 - lon1) * p)) / 2
print(tuple[0][1])
return (int(float(tuple[0][1])), (int(float(tuple[1][1])),12742 * asin(sqrt(a))))
def sort_val(tuple):
dtype = [("globalid", …Run Code Online (Sandbox Code Playgroud) 我有一个集合列表,我希望对 n 个不同的样本进行采样,每个样本都包含每个集合中的一个项目。我不想要的是按顺序排列,因此,例如,我将从第一组中获取所有样本,其中必须包含相同的项目。我也不想创建所有笛卡尔积,因为这在效率方面可能是不可能的......知道如何去做吗?或者甚至可以近似这种行为?
不起作用的示例:
(prod for i, prod in zip(range(n), itertools.product(*list_of_sets)))
Run Code Online (Sandbox Code Playgroud) 我正在尝试获得多个数组的笛卡尔积,但数组非常大,我正在尝试优化内存使用。我尝试使用下面的代码实现一个生成器,但它只是返回在某个位置有一个生成器。
import itertools
x = [[1,2],[3,4]]
def iter_tools(*array):
yield list(itertools.product(*array))
print(iter_tools(*x))
Run Code Online (Sandbox Code Playgroud)
当我尝试相同的代码但用return而不是yield它工作正常时。如何通过实现生成器来获得笛卡尔积?