比较python和rust中的zip-map函数

先看python

a = [1,2,3]
b = [4,5,6]
c = [7,8,9,0]
list(zip(a,b))
list(zip(a,c))

结果为

[(1, 4), (2, 5), (3, 6)]
[(1, 7), (2, 8), (3, 9)]

再看rust

let a = [1, 2, 3];
let b = [4, 5, 6];
	
let d = a.iter().zip(b.iter());
	
for z in d {
    println!("{:?}", z);
}

结果

(1, 4)
(2, 5)
(3, 6)

功能都是类似的

再看看map

map(lambda x: x ** 2, [1, 2, 3, 4, 5])
map(lambda x, y: x + y, [1, 3, 5, 7, 9], [2, 4, 6, 8, 10])

rust 3个迭代

let itr1 = [100, 200, 300, 400, 500, 600];
let itr2 = [10 , 20, 30, 40, 50 ,60];
let itr3 = [1, 2, 3, 4 ,5 , 6];
	
let iter = itr1.iter()
		.zip( itr2.iter())
		.zip( itr3.iter())
		.map(|((x, y), z)| (x, y, z));

for (itr1, itr2, itr3) in iter {
	println!("{} {} {}", itr1, itr2, itr3);
}

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注