2016-04-25 16 views
-1

配列から読み込みしようとしています。作成した配列の中の配列の中の値を返します。見つかったin.thisは現時点でのものです。配列の値を読み込み、条件付きで返す

import pandas as pd 
import os 
import re 

Dir = os.getcwd() 
Blks = [] 

for files in Dir: 
    for f in os.listdir(Dir): 
     if re.search('txt', f): 
      Blks = [each for each in os.listdir(Dir) if each.endswith('.txt')] 
print (Blks) 

for z in Blks: 
    df = pd.read_csv(z, sep=r'\s+', names=['x','y','z']) 
    a = []  
    a = df.pivot('y','x','z') 
    print (a) 

OUTPUTS:配列が隣接する列および行に対応する値である内部

x  300.00 300.25 300.50 300.75 301.00 301.25 301.50 301.75 
y                   
200.00  100  100  100  100  100  100  100  100 
200.25  100  100  100  100  110  100  100  100 
200.50  100  100  100  100  100  100  100  100 

xは、私の列とY行であろう。上記のように、他の値より10上の奇数110の値があります。配列を読み込み、その値をチェックすることによってx(列)とy(行)の値を10の値に戻そうとしていますその横に(上、下、右、左)差を計算します。

誰かが私を正しい方向に導くことができます。初心者のヒントは高く評価されています。私は何を求めているのか不明です。私はすべての方法論で長年の経験がないと尋ねてください。

答えて

0

DataFrame.ixを使用すると、すべての値を1行ずつ行単位でループすることができます。

oddCoordinates=[] 

for r in df.shape[0]: 
    for c in df.shape[1]: 
     if checkDiffFromNeighbors(df,r,c): 
     oddCoordinates.append((r,c)) 
oddCoordinates

に記載されている近隣異なる値の行と列。

隣人との違いを確認するには、あなたは可能性がループして、そこにあるどのように多くの異なる値カウント:

def checkDiffFromNeighbors(df,r,c): 
    #counter of how many different 
    diffCnt = 0 
    #loop over the neighbor rows 
    for dr in [-1,0,1]: 
     r1 = r+dr 
     #row should be a valid number 
     if r1>=0 and r1<df.shape[0]: 
     #loop over columns in row 
     for dc in [-1,0,1]: 
      #either row or column delta should be 0, because we do not allow diagonal 
      if dr==0 or dc==0: 
       c1 = c+dc 
       #check legal column 
       if c1>=0 and c1<df.shape[1]: 
        if df.ix[r,c]!=df.ix[r1,c1]: 
        diffCnt += 1 

    # if diffCnt==1 then a neighbor is (probably) the odd one 
    # otherwise this one is the odd one 
    # Note that you could be more strict and require all neighbors to be different 
    if diffCnt>1: 
     return True 
    else: 
     return False 
関連する問題