簡體   English   中英

通過從 array1 中的所有第一列與 array2 中的所有行查找匹配元素來檢索行元素

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

我在 python 中處於初級水平。 我在從數組中檢索元素時遇到問題。

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)

使用上面的代碼,我設法從文件中獲取所需的數據來執行操作。 現在,我有 2 個 arrays、Vertex_InList 和 Face_array,我必須從中獲取 Vertex_InList 的坐標(第 2 到第 4 列)。 這是通過從 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']

請幫助我找到解決方案。 先感謝您。

我仍然不完全理解這個問題,這是基於我的理解

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM