简体   繁体   English

通过从 array1 中的所有第一列与 array2 中的所有行查找匹配元素来检索行元素

[英]Retrieve row elements by finding matching elements from all first columns in array1 with all rows from array2

I am at a beginner level in python.我在 python 中处于初级水平。 I have a problem retrieving elements from an array.我在从数组中检索元素时遇到问题。

face_splt=[]
with open ('Face_temp.txt','rt') as ifile, open('Face.txt', 'w') as ofile:
    rows = np.array((ifile.readlines()[1::4]))
    for elm in rows:
        face_splt = elm.split()
        Face_array.append(face_splt[2:])
        ofile.write(str(face_splt[2:]))
    ofile.close()

Vertex_array=[]
vertex_splt=[]    
with open ('Vertex_temp.txt','rt') as ifile, open('Vertex.txt', 'w') as ofile:
    rows = np.array((ifile.readlines()[1:]))
    for elm in rows:
        vertex_splt = elm.split()
        Vertex_array.append(vertex_splt[:4])
        ofile.write(str(vertex_splt[:4]))
    ofile.close()

Vertex_in=[]
Vertex_InList=[]
for i in Vertex_array:
    if (((0<float(i[1]))and(float(i[1])<1))and((0<float(i[2]))and(float(i[2])<1))and((0<float(i[3]))and(float(i[3])<1))):
        vertex_id=i[0]
        vertex_x=i[1]
        vertex_y=i[2]
        vertex_z=i[3]
        Vertex_in=[vertex_id,vertex_x,vertex_y,vertex_z]
        Vertex_InList.append(Vertex_in)

Using the above code I managed to take the required data from files to perform the operations.使用上面的代码,我设法从文件中获取所需的数据来执行操作。 Now, I have 2 arrays, Vertex_InList and Face_array, from which I have to take the coordinates from Vertex_InList (columns 2 to 4).现在,我有 2 个 arrays、Vertex_InList 和 Face_array,我必须从中获取 Vertex_InList 的坐标(第 2 到第 4 列)。 This is by finding the matching elements from column 1 in Vertex_InList with all the elements in rows of Face_array.这是通过从 Vertex_InList 的第 1 列中找到与 Face_array 行中的所有元素匹配的元素。

Vertex_InList =
['7', '0.434151598161', '0.327663532237', '0.795784363147']
['10', '0.178728872392', '0.472579545895', '0.429825098041']
['11', '0.186998765476', '0.462472809438', '0.361331160813']
['12', '0.029093767212', '0.415649197792', '0.291312905140']
['18', '0.722421690362', '0.420035938944', '0.196343791871']
['21', '0.434001108634', '0.704989536248', '0.202228280672']
['27', '0.257144028346', '0.573870922957', '0.463532029818']
['30', '0.503947601476', '0.807076535216', '0.272964215305']
['32', '0.181969901270', '0.481439284558', '0.447414063030']
['35', '0.557938786910', '0.677531596182', '0.778820222491']
['39', '0.716230778682', '0.911385466158', '0.401010832367']
['40', '0.893253728733', '0.933628833647', '0.704109823681']
['44', '0.394789918890', '0.407632437561', '0.809129530152']

Face_array = 
['1', '3', '1', '4', '3']
['2', '6', '2', '1', '3', '5', '6', '9']
['3', '3', '2', '9', '8']
['4', '7', '4', '1', '2', '8', '7', '10', '12']
['5', '5', '5', '6', '7', '10', '11']
['6', '5', '5', '3', '4', '12', '11']
['7', '4', '6', '7', '8', '9']
['8', '3', '10', '11', '12']
['9', '5', '20', '13', '3', '4', '23']
['10', '3', '14', '15', '16']
['11', '6', '5', '3', '13', '14', '16', '17']
['12', '6', '19', '15', '14', '13', '20', '22']
['13', '4', '19', '18', '21', '22']
['14', '5', '18', '17', '5', '11', '21']
['15', '5', '15', '16', '17', '18', '19']
['16', '6', '11', '21', '22', '20', '23', '12']
['17', '3', '4', '23', '12']
['18', '5', '25', '24', '20', '23', '31']
['19', '5', '24', '25', '26', '28', '29']
['20', '4', '20', '24', '29', '22']
['21', '4', '27', '26', '28', '30']
['22', '5', '25', '26', '27', '32', '31']
['23', '6', '32', '27', '30', '21', '11', '10']
['24', '5', '28', '29', '22', '21', '30']
['25', '5', '23', '31', '32', '10', '12']
['26', '6', '28', '26', '33', '34', '37', '38']
['27', '3', '33', '36', '34']
['28', '5', '27', '26', '33', '36', '35']
['29', '5', '35', '27', '30', '39', '40']
['30', '5', '35', '36', '34', '37', '40']
['31', '4', '30', '28', '38', '39']
['32', '4', '37', '38', '39', '40']
['33', '4', '41', '25', '31', '42']
['34', '4', '41', '33', '26', '25']
['35', '5', '33', '41', '42', '43', '36']
['36', '4', '32', '27', '35', '44']
['37', '5', '42', '31', '32', '44', '43']
['38', '4', '35', '36', '43', '44']
['39', '5', '45', '46', '47', '49', '48']
['40', '4', '47', '46', '9', '6']
['41', '4', '34', '45', '48', '37']
['42', '7', '9', '46', '45', '34', '36', '43', '8']
['43', '7', '35', '40', '49', '47', '6', '7', '44']
['44', '4', '48', '49', '40', '37']
['45', '4', '7', '44', '43', '8']
['46', '6', '1', '50', '42', '31', '23', '4']
['47', '3', '50', '1', '2']
['48', '5', '42', '50', '2', '8', '43']
['49', '4', '7', '44', '32', '10']
['50', '3', '51', '53', '52']
['51', '5', '38', '53', '51', '29', '28']
['52', '5', '51', '52', '19', '22', '29']
['53', '6', '18', '19', '52', '53', '38', '39']
['54', '4', '39', '18', '21', '30']
['55', '7', '54', '16', '15', '52', '53', '48', '49']
['56', '3', '54', '17', '16']
['57', '4', '48', '53', '38', '37']
['58', '3', '52', '15', '19']
['59', '6', '18', '17', '54', '49', '40', '39']
['60', '3', '54', '49', '47']
['61', '5', '17', '54', '47', '6', '5']

Please help me in finding a solution.请帮助我找到解决方案。 Thank you in advance.先感谢您。

I still dont understand the question fully, this is based on my understanding我仍然不完全理解这个问题,这是基于我的理解

vertex_InList = [['7', '0.434151598161', '0.327663532237', '0.795784363147'],
['10', '0.178728872392', '0.472579545895', '0.429825098041'],
['11', '0.186998765476', '0.462472809438', '0.361331160813'],
['30', '0.503947601476', '0.807076535216', '0.272964215305'],
['32', '0.181969901270', '0.481439284558', '0.447414063030'],
['35', '0.557938786910', '0.677531596182', '0.778820222491'],
['44', '0.394789918890', '0.407632437561', '0.809129530152']]

face_array =[
    ['10', '11', '12'],
    ['30', '32', '35']]


# implementation

solution = []
for row in range(len(face_array)):
    inner = []
    for col in range(len(face_array[row])):
        for vRow in vertex_InList:
            if face_array[row][col] == vRow[0]:
                inner.append(vRow[1:4])
    solution.append(inner)
print(solution)

#output

[[['0.178728872392', '0.472579545895', '0.429825098041'], ['0.186998765476', '0.462472809438', '0.361331160813'], ['0.394789918890', '0.407632437561', '0.809129530152']],
 [['0.503947601476', '0.807076535216', '0.272964215305'], ['0.181969901270', '0.481439284558', '0.447414063030'], ['0.557938786910', '0.677531596182', '0.778820222491']]]



声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取array1中不在array2中的元素 - getting elements in an array1 that are not in array2 Numpy:array1 中也是 array2 元素的元素的掩码 - Numpy: Mask of elements in array1 that are also elements of array2 矩阵中第一行的元素,每行取一个元素,并与行的所有元素进行迭代 - elements from first row in a matrix operated with one element taken from each row, iterating with all the elements of rows 如何从 Python Numpy array1 中提取特定值,并从这些值中创建一个与 array1 形状相同的新 array2 - How to extract specific values from Python Numpy array1 and from those values, create a new array2 with the same shape as array1 Numpy 从 array1 中取出连续的值,将它们放入 array2 中存储在 array3 中的连续索引处 - Numpy take consecutive values from array1, put them into array2 at consecutive indexes stored in array3 从B的所有元素中减去数组A的所有元素? - Subracting all elements of array A from all elements of B? 从 numpy 中的测试数组中找到具有匹配值的数组中所有元素的索引 position - Find the index position of all elements within an array with matching values from a test array in numpy 从数组中提取匹配元素 - Extract matching elements from array numpy从数组下选择所有元素 - numpy select all elements from under array numpy:从数组中选择所有行和列 - Numpy: Select all rows and columns from an array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM