[Tutor] detecting a change in a iterable object (list, array, etc.)
Alan Gauld
alan.gauld at btinternet.com
Tue Dec 18 19:01:41 CET 2007
"Tim Michelsen" <timmichelsen at gmx-topmail.de> 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
More information about the Tutor
mailing list