enter image description here嗨,我有两个数据矩阵,一个是A,另一个是B。我想比较A的第一行和B的第一行,如果在B中找到匹配项,那么应该提取B中的整列。在所附文件中,A、11、12、13和14也可以在B中找到。在这种情况下,B中11、12、13和14下的所有值都被提取到C中。换句话说,A是一个行向量,我想把它和B的第一行进行比较
A = [[11, 12, 13, 14]]
B = [[11, 12, 13, 14, 15, 16],
[2, 1.1, 1.1, 1.3, 1.4, 1.5],
[2, 1.1, 1.1, 1.3, 1.4, 1.5],
[2, 1.1, 1.1, 1.3, 1.4, 1.5],
[2, 1.1, 1.1, 1.3, 1.4, 1.5]]
# Get difference between two lists.
diffs = set(A[0]).symmetric_difference(set(B[0]))
# Get the members index with list comprehension.
indexes = [B[0].index(diff) for diff in diffs]
# Copy B into C. Use copy() otherwise only the reference will be copied.
C = B.copy()
# Delete the columns of C
# This is slow but more readable since you are a beginner in python.
for row in C:
for index in indexes:
del row[index]
print(row)
由此产生:
这段代码远没有经过优化,它只是向您展示实现结果的几个步骤
相关问题 更多 >
编程相关推荐