【发布时间】:2019-09-03 14:28:39
【问题描述】:
我有两个列表,它们是至少有 100 行的二维列表。我想将 A 匹配到 B,反之亦然。但真正的问题不是从 A 逐行输入以匹配 B。有没有更快的方法来循环 B 中的所有行以匹配 B 中的所有行?
我尝试了 a[0] 和 a[1] 和 a[2]。这种方法会起作用,但我必须逐行输入很多内容。这将是很多打字,尤其是如果它有很多行?
我有两个二维列表。我想匹配 A 中的任何值以匹配 B 中的任何值
a=[[9, 15, 25],[4, 14, 18, 25],[11, 12, 24, 25],[4, 8, 9, 26]]
b=[[2, 4, 7, 13, 14],[3, 5, 8, 13, 14],[6, 9, 10, 13, 14],[5, 6, 7, 13, 15],[3, 4, 9, 13, 15],[2, 8, 12, 13, 15],[4, 6, 8, 14, 15],[2, 5, 9, 14, 15]]
b0 = [list(filter(lambda x: x in a[0], sublist)) for sublist in b]
b1 = [list(filter(lambda x: x in a[1], sublist)) for sublist in b]
b2 = [list(filter(lambda x: x in a[2], sublist)) for sublist in b]
b3 = [list(filter(lambda x: x in a[3], sublist)) for sublist in b]
有没有更快的方法来循环遍历所有行?特别是如果有很多行
【问题讨论】:
-
你的预期输出是什么?
-
请分享您期望的输出
-
0.[[], [], [9], [15], [9, 15], [15], [15], [9, 15]] 1. [[4 , 14], [14], [14], [], [4], [], [4, 14], [14]] 2. [[], [], [], [], [], [12], [], []] 3. [[4], [8], [9], [], [4, 9], [8], [4, 8], [9]]。我想找到与 A 中的行号匹配的值。
标签: python list arraylist pattern-matching match