.csv 파일로 내보내려면 테이블 스타일 행렬에서 XYZ 좌표를 정렬하고 배열해야합니다.파이썬 정렬 테이블 배열의 XYZ 좌표 배열
사용자 Michael0x2a의 도움으로 어느 정도 관리 할 수있었습니다. 나는 X와 Y를 반복 한 경우 내 문제는 지금은 예를 실행하는 경우는, 행
example = [[1, 1, 20], [1, 1, 11], [2, 3, 12.1], [2, 5, 13], [5,4,10], [3,6,15]]
main(example)
0, 1, 1, 3, 4, 5, 6
1, 0, 11, 0, 0, 0, 0
2, 0, 0, 12.1, 0, 13, 0
3, 0, 0, 0, 0, 0, 15
5, 0, 0, 0, 10, 0, 0
그래서 열 머리글은 Y 값입니다 반환합니다 아래는 Z를
def find_x_and_y(array):
"""Step 1: Get unique x and y coordinates and the width and height of the matrix"""
x = sorted(list(set([i[0] for i in array])))
y = sorted(list([i[1] for i in array]))
height = len(x) + 1
width = len(y) + 1
return x, y, width, height
def construct_initial_matrix(array):
"""Step 2: Make the initial matrix (filled with zeros)"""
x, y, width, height = find_x_and_y(array)
matrix = []
for i in range(height):
matrix.append([0] * width)
return matrix
def add_edging(array, matrix):
"""Step 3: Add the x and y coordinates to the edges"""
x, y, width, height = find_x_and_y(array)
for coord, position in zip(x, range(1, height)):
matrix[position][0] = coord
for coord, position in zip(y, range(1, width)):
matrix[0][position] = coord
return matrix
def add_z_coordinates(array, matrix):
"""Step 4: Map the coordinates in the array to the position in the matrix"""
x, y, width, height = find_x_and_y(array)
x_to_pos = dict(zip(x, range(1, height)))
y_to_pos = dict(zip(y, range(1, width)))
for x, y, z in array:
matrix[x_to_pos[x]][y_to_pos[y]] = z
return matrix
def make_csv(matrix):
"""Step 5: Printing"""
return '\n'.join(', '.join(str(i) for i in row) for row in matrix)
def main(array):
matrix = construct_initial_matrix(array)
matrix = add_edging(array, matrix)
matrix = add_z_coordinates(array, matrix)
print make_csv(matrix)
0을 반환 할 것이다 헤더는 x 값입니다.
[1,1,20]의 첫 번째 집합에 대해 두 번째 집합 [1,1,11] 때문에 1,1,0을 반환합니다. 동일한 x 및 y 값을가집니다.
최종 결과는 다음과 같아야합니다
이x_to_pos = dict(zip(x, range(1, height)))
y_to_pos = dict(zip(y, range(1, width)))
이 사람이 나를 도울 수 :
0, 1, 1, 3, 4, 5, 6
1, 20, 11, 0, 0, 0, 0
2, 0, 0, 12.1, 0, 13, 0
3, 0, 0, 0, 0, 0, 15
5, 0, 0, 0, 10, 0, 0
내가이이 기능을 함께 할 수있는 뭔가가 생각?
정말 고마워요
시스코 여기
가 예상 결과 무엇 첫 번째 열 (행 인덱스) 슬라이스되어야한다? 같은 좌표에 대해 두 개의 다른 값이있는 경우 두 좌표를 한 위치에 맞출 수 없습니다. 또는 ... 튜플로 할 수 있습니다 - 원하는 경우? – BartoszKP
질문을 편집하여 최종 결과가 – user2725701
이어야한다고 추가하십시오. 질문을 편집하고 예상 결과를 포함하십시오. 그렇게하면 행렬을 제대로 포맷 할 수 있습니다. – thefourtheye