Skip to content Skip to sidebar Skip to footer

How Can I Sort Through A Range In A Csv File In 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. ex. (depth) 88, 77,

Solution 1:

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 numbersfor i inrange(lengh): # we run our code for each number inside our list of numberifnot 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 lineif 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})
   breakelse:
   group +=1
   splited_list.update({str(group):[l[i]]})
   break

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

Solution 2:

This task is straight forward with numpy.diff() and numpy.where() as:

Code:

import numpy as np

defsplit_at_mins(a_list):
    end_points = list(1 + np.where(0 < np.diff(a_list))[0])
    return [a_list[i:j] for i, j inzip([0] + end_points, end_points + [len(a_list)])]

Test Code:

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)))

Produces:

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

Post a Comment for "How Can I Sort Through A Range In A Csv File In Python?"