Re: [Tutor] detecting a change in a iterable object (list, array, etc.)
"Tim Michelsen" <[EMAIL PROTECTED]> wrote >> A list comprehension will work for this. If data is a list of >> triples of >> (year, month, volume) then this will give you a list of the 1997 >> triples: >> >> data1997 = [ item for item in data if item[0]==1997 ] Note4 that for this to work it assumes a *list of triples* > I tried your code out (see below). > for line in input_data: >#~ print line >#~ year month day temp_3hr >year = int(line[1]) >month = int(line[2])-1 >day = int(line[3]) >value = float(line[6])*0.5 >compact_list = [year, month, day, value] You are overwriting the same list with a new set of values so your final list is just the last set of 4 values. I suspect you meant to have compact_list.append( (year,month,day,value) ) >res_rows = [ item for item in compact_list if compact_list[0] == > 1990 ] And this becomes the same as the original suggestion res_rows = [ item for item in compact_list if item[0] == 1990 ] If I understand what you are trying to do... HTH, -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] detecting a change in a iterable object (list, array, etc.)
Hello, > A list comprehension will work for this. If data is a list of triples of > (year, month, volume) then this will give you a list of the 1997 triples: > > data1997 = [ item for item in data if item[0]==1997 ] I tried your code out (see below). Here is the output: [] [] [] [] [] [1990, 0, 1, -18.0] [1990, 0, 2, -0.5] [1990, 0, 3, -14.0] [1990, 0, 4, -21.0] How do I avoid the []? What whould be the easiest way to save this list (without the "[]") into a file and append a newline after each row? Thanks for your help in advance, Timmie Here's the code: #!/usr/bin/env python # currently used modules import csv # SOME VARIABLES #~ stattion_name = 'Sosan' input_file_name = '../data/filter_test_data.csv' output_file_name = '../data/filter_test_data_result.csv' # prepare files input_file = open(input_file_name, 'r') output_file = open(output_file_name ,'w') header = u"Year, Month, Day, Hour_of_day [h], values, \n" input_data = csv.reader(input_file, delimiter=';') #~ skip the first rows line1 = input_file.readline() line2 = input_file.readline() #~ write the header output_file.write(header.encode('utf-8')) counter = 0 value_col = 6 for line in input_data: #~ print line #~ year month day temp_3hr year = int(line[1]) month = int(line[2])-1 day = int(line[3]) value = float(line[6])*0.5 compact_list = [year, month, day, value] res_rows = [ item for item in compact_list if compact_list[0] == 1990 ] print res_rows input_file.close() output_file.close() ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] detecting a change in a iterable object (list, array, etc.)
Hello, > If you show us what you have done so far it would be easier to make > suggestions. The thing is that I am working a lot with time series data and need to write criteria based filters for that data. There's already a start in SciPy Time Series package: http://www.scipy.org/SciPyPackages/TimeSeries But it is still in early development. Your suggestions are exactly what I was looking for. A kick into the right direction... I found itertools when writing my code for the last list items: http://groups.google.com/group/comp.lang.python/browse_thread/thread/34c7398eec5a92cd/68f0aaef68e5ca0e?lnk=raot I will keep you upodated it when I finshed investigating and incorporating your tips. Thanks so far, Timmie ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] detecting a change in a iterable object (list, array, etc.)
Tim Michelsen wrote: > Hello, > would like to ask for your help on the following issue: > What procedure can I use to detect if there's a change when iterating > over a list? > > For instance if I want to extract the years 1997 and 1998 from the table > below and save them into separate files? A list comprehension will work for this. If data is a list of triples of (year, month, volume) then this will give you a list of the 1997 triples: data1997 = [ item for item in data if item[0]==1997 ] > How do I build the average only on the 1997-year values? Given the above data1997 list can you do this? Sum the third values and divide by the length of the list. > Or how do find out that there is three successive values 3*2 and 3*2 in > the volume column? itertools.groupby() is helpful for this. It's a bit tricky to understand though. Here is an extended example: http://personalpages.tds.net/~kent37/blog/arch_m1_2005_12.html#e69 Here is an example that shows sequences of values of length 3 or more: import itertools def key(item): return item[2] for k, g in itertools.groupby(data, key=key): g = list(g) if len(g) > 2: print k, 'occurs', len(g), 'times' > > I already tried a for-loop in connection with a last_value == current > value comparison but wasn't successful at all. If you show us what you have done so far it would be easier to make suggestions. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] detecting a change in a iterable object (list, array, etc.)
Hello, would like to ask for your help on the following issue: What procedure can I use to detect if there's a change when iterating over a list? For instance if I want to extract the years 1997 and 1998 from the table below and save them into separate files? How do I build the average only on the 1997-year values? Or how do find out that there is three successive values 3*2 and 3*2 in the volume column? I already tried a for-loop in connection with a last_value == current value comparison but wasn't successful at all. Therefore, I highly appreaciate any hint or pointer! Example data: Yearmonth volume 19971 2 19972 2 19973 2 19974 5 19975 2 19977 1 19981 2 19982 6 19983 3 19984 3 19985 3 19986 1 Thanks and kind regards, Tim Michelsen ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor