I am not clear about it can help you, because I don't understand very well your question, maybe I need more info. However, if each vertex have 3 coords or numbers like I write in the example, here is a solution.
# Import the dependencies
import numpy as np
# Assuming content is some like that, each vertex have three dimensions
content = [
'vertex 1.0 2.0 3.0',
'vertex 4.0 5.0 6.0',
'vertex 9 10 11'
]
# Extract the coordinates of the vertices
# Split each line, take elements after 'vertex', convert to float, and create a NumPy array
vertices = np.array([line.split()[1:] for line in content if line.startswith('vertex')], dtype=float)
# Verify if the size of the array is a multiple of 3
# Reshape the array to convert it to shape (n, 3)
vertices = vertices.reshape((-1, 3))
# Print the reshaped array
print(vertices)
If this doesn't is the situation, 13746/3 = 4582. So, you can do the following:
vertices = np.reshape(vertices,(-1, 3))
NumPy calculate the necessary rows to reshape with 3 columns/dimensions.
About your question, I think that is impossible to reshape (n,) to (n,3), unless do you want to repeat the entries?
line
s incontent
actually look like, the approach to achieve this could vary. Could you provide an excerpt ofcontent
to see what your data looks like?reshape
cannot change the total number of elements of an array. You can reshape to (n,1), e.g withvertices[:,None]
. Due tobroadcasting
rules that may be enough. To get(n,3)
shape, you'll need to usenp.repeat
.