Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select entry from array given another value

I have a text file with the format (date, time, resistance):

12/11/2013  13:20:38    28.321930E+3
...         ...             ...

I need to extract the value of resistance (third column) from every 6 seconds after the first data entry. To start I wanted to import the text file using:

date, time, resistance = loadtxt('Thermometers.txt', unpack=True, usecols=[0,1,2])

However before I've hardly begun my program, I get the error:

ValueError: invalid literal for float(): 12/11/2013

-ALSO-

I am not sure how to also iterate through time given that the date changes as it's an over-night data run. Elegant solutions to my problem(s) would be much appreciated.

like image 321
user2992169 Avatar asked Nov 14 '13 13:11

user2992169


People also ask

How do you find the next value in an array?

The next() function moves the internal pointer to, and outputs, the next element in the array. Related methods: prev() - moves the internal pointer to, and outputs, the previous element in the array. current() - returns the value of the current element in an array.

Can values of one array be assigned to another?

Ensure that the two arrays have the same rank (number of dimensions) and compatible element data types. Use a standard assignment statement to assign the source array to the destination array. Do not follow either array name with parentheses.

What is the use of In_array ()?

The in_array() function is an inbuilt function in PHP that is used to check whether a given value exists in an array or not. It returns TRUE if the given value is found in the given array, and FALSE otherwise.


1 Answers

I think this code will do what you want to do. And also, you don't have to worry about the overnight data and changing date since this converts it to datetime object.

    import datetime

    filtered_data=[]

    my_data=open(my_file,'r')
    for line in my_data:

        data_arr=line.split()
        dte=data_arr[0].split("/") r
        tme=data_arr[1].split(":") 
        new_date=datetime.datetime((int(dte[2]),int(dte[0]),int(dte[1]),
                                    int(tme[0]),int(tme[1]),int(tme[2]))

        if filtered_data==[]:
           filtered_data.append(data_arr)

        else:
           if (new_date-old_date).seconds==6:
                filtered_data.append(data_arr)

        old_date=new_date

This will give you a list where the items are filtered as per your situation ( in every 6 seconds). Now if you just want the array of your resistance which are distributed at 6 seconds interval, using simple loop or list comprehension like below will suffice:

R_in_six_sec_interval=[R[2] for R in filtered_data]
like image 67
Jack_of_All_Trades Avatar answered Sep 28 '22 12:09

Jack_of_All_Trades