iterating over the other and finding the greatest

Tim Chase python.list at tim.thechases.com
Sun Oct 28 09:01:08 EDT 2007


> have carry out a process a>b then i should print the line and if b>c then i
> should print the line and c>d then i should print....... like this i have to
> continue.say for eg: 43<387 so the first row is omitted, 387 is greater then
> 98 so i can print the line second row...
> my code:
>  fh = open('364010_spacing','r')
> for lines in fh.readlines():
>         data = lines.strip().split('\t')
>         start =data[0].strip()
>         end = data[1].strip()
>         values = data[2].strip()
>         id = data[3].strip()
>         if a > b :#hanged up here
>                         print lines

though you don't really define a/b/c anywhere, other than
obliquely referencing values in your 3rd column, I presume they
map to the lines in the file a=1st-line, b=2nd-line, etc.

You also don't disclose when you want the printing to happen,
whether you want to print the highest at each line as you iterate
over them (in your example, printing a,b,b,b,e,e,e,e or if you
want to find the maximum and then print it at the end (just
printing line e once).

I don't know if you want to do if more than one row has the
maximum value.  Do you want to print all of the matching lines?
or just the first or last one encountered?

The code below iterates over the file, finding the maximum of the
3rd column, and then printing the first line that matched that.

  for i,line enumerate(open(filename, 'r')):
    c  = int(line.rstrip('\n').split('\t')[2])
    if i == 0:
      highest_id = c
      best_match = line
    else:
      if c > highest_id:
        highest_id = c
        best_match = line
  print line

In your code, you parsed start/end, but didn't use them for
anything...vestigial code?

-tkc









More information about the Python-list mailing list