
1 cassidyhere Apr 22, 2021 from itertools import product product(a, b) |
2 abersheeran Apr 22, 2021 via Android 你这不叫合并,你这是算笛卡尔积。循环是少不了的,但是写法可以不一样。用 map 或者推导式。 |
3 ZoeYn OP @cassidyhere 试了一下,可以!! |
4 ZoeYn OP @abersheeran 搜嘎!我去补一补这个知识,谢谢! |
5 JackCooper Apr 22, 2021 ruby 的话 2.5.7 :001 > a = [1,2,3,4] => [1, 2, 3, 4] 2.5.7 :002 > b = ['a', 'b' ,'c', 'd'] => ["a", "b", "c", "d"] 2.5.7 :003 > a.zip(b) => [[1, "a"], [2, "b"], [3, "c"], [4, "d"]] |
6 hyrious Apr 22, 2021 @JackCooper a.product(b) |
7 encounter2017 Apr 22, 2021 python 的话 from itertools import product a = [1,2] b = ['a','b','c'] list(product(a,b)) # [(1, 'a'), (1, 'b'), (1, 'c'), (2, 'a'), (2, 'b'), (2, 'c')] scala 的话 scala> for { | a <- List(1, 2) | b <- List('a', 'b', 'c') | } yield (a,b) val res1: List[(Int, Char)] = List((1,a), (1,b), (1,c), (2,a), (2,b), (2,c)) |
8 bxb100 Apr 22, 2021 Numpy 不是一堆矩阵方法吗 |
9 Codewj Apr 22, 2021 |
10 cyspy Apr 22, 2021 [(x, y) for x in a for y in b] |
11 Lordon Apr 22, 2021 可以参考 leetcode17 题 形式上很像 |
12 css3 Apr 22, 2021 ret = [[str(x) + y] for x in a for y in b] |
13 ZoeYn OP |
14 raaaaaar Apr 23, 2021 都是 O(n^2) 的,数据库连接操作就以这个为基础,如果能优化数据库早干了 |
15 liuxingdeyu Apr 23, 2021 这。。。笛卡尔积无解,就得一个一个算,无非就是写法上优雅点,map 、reduce 、lambda 啥的 |
16 HashV2 Apr 23, 2021 itertools 库看一遍吧,方法不多 但是都很好用 |
17 sugarkeek Apr 23, 2021 就算是库也是这么一个一个合并的 |
18 ZoeYn OP |