【发布时间】:2019-10-14 14:47:33
【问题描述】:
我有两个数组,我想遍历第二个数组,只返回第一个元素等于另一个数组中的元素的数组。
a = [10, 11, 12, 13, 14]
b = [[9, 23, 45, 67, 56, 23, 54], [10, 8, 52, 30, 15, 47, 109], [11, 81,
152, 54, 112, 78, 167], [13, 82, 84, 63, 24, 26, 78], [18, 182, 25, 63, 96,
104, 74]]
我有两个不同的数组,a 和 b。我想找到一种方法来查看 b 中的每个子数组(?),其中 第一个值等于数组 a 中的值以创建一个新数组 c。
我要找的结果是:
c = [[10, 8, 52, 30, 15, 47, 109],[11, 81, 152, 54, 112, 78, 167],[13, 82, 84, 63, 24, 26, 78]]
Python 有没有一种工具可以以 Excel 的 MATCH() 的方式执行此操作?
我尝试以如下方式循环:
for i in a:
if i in b:
print (b)
但是因为数组中还有其他元素,所以这种方式是行不通的。任何帮助将不胜感激。
问题进一步说明:
a = [5, 6, 7, 9, 12]
我使用 XLRD (b_csv_data) 读取了一个 excel 文件:
Start Count Error Constant Result1 Result2 Result3 Result4
5 41 0 45 23 54 66 19
5.4 44 1 21 52 35 6 50
6 16 1 42 95 39 1 13
6.9 50 1 22 71 86 59 97
7 38 1 43 50 47 83 67
8 26 1 29 100 63 15 40
9 46 0 28 85 9 27 81
12 43 0 21 74 78 20 85
接下来,我创建了一个可以读取选定行数的外观。为简单起见,上面的这个文件只有几行。我当前的文件大约有 100 行。
for r in range (1, 7): #skipping headers and only wanting first few rows to start
b_raw = b_csv_data.row_values(r)
b = np.array(b_raw) # I created this b numpy array from the line of code above
【问题讨论】:
标签: python arrays loops numpy indexing