如何在python的csv文件中对范围进行排序? [英] How can I sort through a range in a csv file in python?

查看:170
本文介绍了如何在python的csv文件中对范围进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题:我有一个csv文件,该文件的数据深度在90到3米之间,并且数据将像这样来回移动.我正在使用最新的python.

My problem: I have a csv file that has data that goes between 90 to 3 meters deep, and that data will go back and forth like so. I am using the newest python.

例如(深度)88、77、50、20、5、90、76、54、34、15、8、4、81、74、62、51、49、30、22、10、8 ...等.保持从90到3,然后再返回.

ex. (depth) 88, 77, 50, 20, 5, 90, 76, 54, 34, 15, 8, 4, 81, 74, 62,51, 49, 30, 22, 10, 8... and so on. It keeps goes from 90 to 3, and back again.

我想要做的是每次在90到3之间移动数据.一旦分离,我想获取列表中的最后一个值和第一个值. 像这样 前任. 88、77、50、20、5(此处分隔),90、76、54、34、15、8、4(此处分隔)81、74、62,51、49、30、22、10、8此处分隔)... 等等.保持从90到3,然后再返回.

What I want to do is to separate the data each time it goes between 90 and 3. Once it is separated I want to take the last and first values in that list. Like so ex. 88, 77, 50, 20, 5 (separate here), 90, 76, 54, 34, 15, 8, 4 (separate here) 81, 74, 62,51, 49, 30, 22, 10, 8 separate here)... and so on. It keeps goes from 90 to 3, and back again.

我该怎么做?或者,我将如何将它们分成列表,然后使用它们各自的数据?

How do I go about doing this? Or how would I separate them into them into lists and then use data from each of them?

这是我到目前为止的代码:

Here's the code I have so far:

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)

推荐答案

好吧,您的代码有点复杂,我也不知道numpy,无论如何,这是我想出的用于分隔数字范围的解决方案:

well your code is a little complicated and i don't know numpy, anyway this is a solution i came up with for separating range of number:

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分开,它给出以下输出:

it separate range 90 to 3 as you wanted ,it gives this output:

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

这篇关于如何在python的csv文件中对范围进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆