2013-08-29 5 views
1

.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 

내가이이 기능을 함께 할 수있는 뭔가가 생각?

정말 고마워요

시스코 여기

+0

가 예상 결과 무엇 첫 번째 열 (행 인덱스) 슬라이스되어야한다? 같은 좌표에 대해 두 개의 다른 값이있는 경우 두 좌표를 한 위치에 맞출 수 없습니다. 또는 ... 튜플로 할 수 있습니다 - 원하는 경우? – BartoszKP

+0

질문을 편집하여 최종 결과가 – user2725701

+0

이어야한다고 추가하십시오. 질문을 편집하고 예상 결과를 포함하십시오. 그렇게하면 행렬을 제대로 포맷 할 수 있습니다. – thefourtheye

답변

0

는 제안이다. key 매개 변수가있는 rangesorted 함수를 사용하여 xy을 나중에 정렬하는 데 필요한 색인을 얻습니다 (자세한 내용은 'How to get indices of a sorted array in Python' 참조). 이것은 자동으로 중복 값을 처리합니다.

example = [[1, 1, 20], [1, 1, 11], [2, 3, 12.1], [2, 5, 13], [5,4,10], [3,6,15]] 
x = [el[0] for el in example] 
y = [el[1] for el in example] 
z = [el[2] for el in example] 

# indices for x,y to get them in sorted order later 
# duplicates in both dimensions are preserved 
x_idx = sorted(range(len(x)), key=lambda k:x[k]) 
y_idx = sorted(range(len(y)), key=lambda k:y[k]) 

# initialize A with 0 
A = [[0 for _ in range(len(y)+1)] for _ in range(len(x)+1)] 

# and fill it with values 
for k, val in enumerate(z): 
    A[x_idx[k]+1][y_idx[k]+1] = val 
    A[k+1][0] = x[x_idx[k]] 
    A[0][k+1] = y[y_idx[k]] 

그러나이 스크립트의 결과는 원하는대로 (아직) 제공되지 않습니다. A는 결국 다음과 같습니다 중복 값 1이 새 열뿐만 아니라, 새 행을 만들뿐만 아니라

[[0, 1, 1, 3, 4, 5, 6], 
[1, 20, 0, 0, 0, 0, 0], 
[1, 0, 11, 0, 0, 0, 0], 
[2, 0, 0, 12.1, 0, 0, 0], 
[2, 0, 0, 0, 0, 13, 0], 
[3, 0, 0, 0, 0, 0, 15], 
[5, 0, 0, 0, 10, 0, 0]] 

하는 것으로.

가정 :동일한 인덱스의 행은 병합되어야합니다. 이것은 itertools groupby 함수를 사용하고 zip + sum을 사용하여 행을 단순히 합쳐서 "병합"할 수 있습니다.

이 목록 AA의 결과 목록은 다음과 같습니다
AA = [] 
for row_index, rows_to_be_merged in itertools.groupby(A, lambda x: x[0]): 
    AA.append([row_index] + 
       [sum(rows) for rows in zip(*rows_to_be_merged)][1:]) 

:

[[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]]