2016-10-27 1 views
0

내 스크립트로 솔루션을 얻으려면 도움이 필요합니다. 두 개의 배열로 값 바꾸기 numpy

나는 두 배열을 가지고 :

==> 첫 번째는 code_client라는 1 열을 얻는다. 파일 이름은 excel.txt입니다. ==> 두 번째 열은 rowidcode_client의 두 열이 있습니다. 우리는 파일을 BDD.txt이라고 부를 것입니다.

첫 번째 파일에서 두 번째 파일과 관련된 rowid 값을 바꾸고 싶습니다.

code_client 
208 
1643 
1595 
2607 
2608 
2470 
1547 
481 
226 
558 

두 번째 파일이 보이는 같은 : 내가 값을 연결할 수 있습니다 방법을 모르는

# Script permettant de remplacer le code_client par le rowid de la BDD 

# coding: utf8 

import numpy as np 

# Importation du fichier excel 

excel = np.loadtxt('/Users/valentinjungbluth/Desktop/excel.txt', 
        dtype = int) 

#print excel 

BDD = np.genfromtxt('/Users/valentinjungbluth/Desktop/BDD.txt', 
        dtype = [('rowid','i8'),('code_client','i8')], 
        delimiter = ',') 

for row1 in excel : 
    if excel == BDD['code_client'] : 
     print 'oui' 
    else : 
     print 'non' 

:

rowid,code_client 
1,1 
2,2 
3,4 
4,5 
5,6 
6,7 
7,8 
8,9 
9,10 
10,11 
11,12 
12,13 
13,14 
14,15 

이 내 스크립트처럼

먼저 파일을 찾습니다 . excel file에서 행을 읽고 BDD file과 비교해야한다고 생각합니다. 에서 BDD file으로 동일한 번호를 얻은 경우 을 excel file에서 으로 바꿉니다. 그렇지 않으면 계속하겠습니다.

도움이 될까요?

감사합니다.

편집 : 예상 된 FILE

첫 번째 파일이되고 있습니다

code_client ==> rowid 
208 ==> this code_client in the second file gives rowid = 183 
1643 ==> this code_client in the second file gives rowid = 1498 
1595 ==> ... 
2607 
2608 
2470 
1547 
481 
226 
558 
+0

첫 번째 파일에'list '행이 있고'index_ 1'과 동일한'row_id'가 아니라면 그냥 호출 할 수 있습니다 'list [row_id - 1] = new_value' –

+0

샘플의 예상 출력은 얼마입니까? – Divakar

+0

@btquanto 첫 번째 파일이 오름차순으로 정렬되지 않았으므로 아니요. 나는이 명령서를 지켜야한다. 또한, rowid = 1,2,3 ... 및 code_client = 1,2,4 등. 끝에서 rowid = 2800 및 code_client = 3600 – Deadpool

답변

1

내가 NumPy와이 무엇을 잘 모르겠어요,하지만 난 여기 추측 당신이 원하는 무엇

dictionary = dict() 
for data in BDD: 
    # We are building a dictionary to associate the code_client with rowid 
    dictionary[data['code_client']] = data['rowid'] 
for code_client in excel: 
    # From the code_client, you can get the rowid from the dictionary 
    row_id = dictionary.get(code_client, None) 
    print code_client, "=>", row_id # Do what you want 

당신에게 다음과 같이 을 만들 수도 있습니다.

dictionary = dict([(data['code_client'], data['row_id']) for data in BDD]) 

귀하의 BDDlistdict이고 키가 rowid이고 code_client이라고 가정합니다. 그리고 excel은 값 목록입니다

+0

업데이트했습니다. 두 파일 모두 값 목록입니다. 귀하의 스크립트를 사용해 보겠습니다. – Deadpool

+0

작동하지 않습니다. 마지막 행의 구문 오류입니다. 나는 작동하지 않는 것을 볼 것이다. 출력 파일에, 난 juste code_client 값 대신 rowid 값을 얻을 필요가;) – Deadpool

+0

그냥 구문 오류를 해결 : D –

관련 문제