2017-02-25 2 views
1

私の問題:私はのファイルに90〜3メートルの深さのデータがあり、そのデータは前後に行き来します。私は最新のPythonを使用しています。csvファイルの範囲をソートするにはどうすればよいですか?

ex。 (深さ)88,77,50,20,5,90,76,54,34,15,8,4,81,74,62,51,49,30,22,10,8 ...など。それは90から3へと戻り、再び戻ります。

私がしたいことは、90と3の間でデータを区切ることです。分離されたら、そのリストの最後と最初の値を取りたいと思います。 ように ex。 90,76,54,34,15,8,4(ここでは分離しています)81,74,62,51,49,30,22,10,8ここでは、 )... 等々。それは90から3へと戻り、再び戻ります。

これを行うにはどうすればよいですか?または、それらをリストに分けてそれぞれのデータを使用するにはどうすればよいですか?

import csv, numpy 
from collections import defaultdict 

columns = defaultdict(list) # each value in each column is appended to a list 

with open('C:\\Users\\AdamStoer\\Documents\\practicedata.csv') as f: 

reader = csv.DictReader(f,delimiter=',') # read rows into a dictionary format 
for row in reader: 
    r = float(row['roll']) 
    p = float(row['pitch']) 
    if 0.21 <= p <= 0.31: 
      if -0.06 <= r <= 0.06: 
       columns['pitch'].append(row['pitch']) 
       columns['roll'].append(row['roll']) 
       columns['i_depth'].append(row['i_depth']) 
       columns['irrad2'].append(row['sci_ocr504i_irrad2']) 

print ('Pitch:') 
print (columns['pitch']) 
print ('Roll:') 
print (columns['roll']) 
print ('Depth') 
print (columns['i_depth']) 
print ("Irrandiance(2):") 
print (columns['irrad2']) 


irradlst = columns['irrad2'] 
irradfirst = irradlst[0] 
irradlast = irradlst[-1] 

depthlst = columns['i_depth'] 
depthfirst = depthlst[0] 
depthlast = depthlst[-1] 

print ("\nDepth 1 is " + depthfirst + " and " + "Depth 2 is " + depthlast) 

print ("\nIrradiance 1 is " + irradfirst + " and " + "Irradiance 2 is " +  irradlast) 

#Find the Volume Attenuation Coefficient 

#irranddiv = deepest/shallowest 
irraddiv = float(irradfirst)/float(irradlast) 

#depthdif = deepest-shallowest 
depthdif = float(depthfirst) - float(depthlast) 

#Find Log of irraddiv 
irradlog = numpy.log(irraddiv)   

#Find K 
K = irradlog/(-depthdif) 

print("\nAttenuation Coefficient") 
print (K) 

答えて

0

あなたのコードは少し複雑で、私はnumpyのを知らない、とにかくこれは私が数の範囲を分離するために思い付いたソリューションであるだけでなく:

ここ

は、私がこれまで持っているコードです

l = [88, 77, 50, 20, 5, 90, 76, 54, 34, 15, 8, 4, 81, 74, 62,51, 49, 30, 22, 10, 8,65] 

group =0 #since were using dictionaries i use group number as dictionary KEY to distinguish each group of number between 3 to 90 
temp = [] # this is a list that we keep temporary group of our number ranged between 3 to 90 
splited_list = {} #this is a dictionary that we keep our final result in side it 
lengh = len(l) #this is a lengh of our list of numbers 

for i in range(lengh): # we run our code for each number inside our list of number 
if not i == lengh-1: # at this line we check if our number is not the last number in list , because in next line we check our number with the number that comes after our current number and if it's the last number we get "IndexError: list index out of range" error at next line 
    if l[i] > l[i+1]: # since our range is between 3 to 90 , so if our current number is bigger than next number, for sure it is bigger than 3, so it is between 3 to 90 and we can add it to our current group of number 
    temp.append(l[i]) 
    else: #if it is not bigger than the next number it is our last number in our current group of number in range of 90 to 3 so we add it to our temp 
    temp.append(l[i]) 
    group +=1 
    splited_list.update({str(group):temp}) 
    temp = [] 
else: # when we reach this line it means that we get to the last number in ourlist , since there is no next number , we check it with previous number , if its bigger it is not belong to current group of number between 3 to 90 and if it's smaller it is belong to the current group of number 
    if l[i] < l[-2]: 
    temp.append(l[i]) 
    group +=1 
    splited_list.update({str(group):temp}) 
    break 
    else: 
    group +=1 
    splited_list.update({str(group):[l[i]]}) 
    break 

それ別の範囲90〜3あなたが望んでいたとして、それはこの出力を与える:

>>> splited_list 
{'2': [90, 76, 54, 34, 15, 8, 4], '1': [88, 77, 50, 20, 5], '4': [65], '3': [81, 74, 62, 51, 49, 30, 22, 10, 8]} 
+0

あなたの答えをありがとう! – Adam

+0

あなたのコードを少し説明できるかどうか疑問に思っていましたか? – Adam

+0

@Adam私はいくつかのコードの説明を追加しました – ali

0

このタスクは、STRありますaightフォワードとしてnumpy.diff()numpy.where()有する:

コード:

import numpy as np 

def split_at_mins(a_list): 
    end_points = list(1 + np.where(0 < np.diff(a_list))[0]) 
    return [a_list[i:j] for i, j in 
      zip([0] + end_points, end_points + [len(a_list)])] 

テストコード:

test_data = (
    88, 77, 50, 20, 5, 
    90, 76, 54, 34, 15, 8, 4, 
    81, 74, 62, 51, 49, 30, 22, 10, 8 
) 

print('\n'.join(str(d) for d in split_at_mins(test_data))) 
print('\n'.join('%s %s' % (d[0], d[-1]) for d in split_at_mins(depth))) 

生成し:

(88, 77, 50, 20, 5) 
(90, 76, 54, 34, 15, 8, 4) 
(81, 74, 62, 51, 49, 30, 22, 10, 8) 
88 5 
90 4 
81 8 
+0

あなたの答えをありがとう! – Adam

関連する問題