[Tutor] Please help matching elements from two lists and
printing them
Bob Gailer
bgailer at alum.rpi.edu
Wed Dec 8 23:57:43 CET 2004
At 02:51 PM 12/8/2004, kumar s wrote:
>Dear group,
>
> I have two tables:
>
>First table: spot_cor:
>432 117
>499 631
>10 0
>326 83
>62 197
>0 0
>37 551
>
>
>
>Second table: spot_int
>0 0 98
>1 0 5470
>2 0 113
>3 0 5240
>4 0 82.5
>5 0 92
>6 0 5012
>7 0 111
>8 0 4612
>9 0 115
>10 0 4676.5
>
>
>
>I stored these two tables as lists:
>
> >>> spot_cor[0:5]
>['432\t117', '499\t631', 10\t0', '326\t83', '62\t197']
Note there is no ' before the 10. That won't fly'
> >>> spot_int[0:5]
>[' 0\t 0\t18.9', ' 1\t 0\t649.4', ' 10\t
>0\t37.3', ' 3\t 0\t901.6', ' 4\t 0\t14.9']
It would be a lot easier to work with if the lists looked like (assumes all
data are numeric):
[(432,117), (499,631), (10,0), (326,83), (62,197)]
[(0,0,18.9), (1,0,649.4), (10,0,37.3), (3,0,901.6), (4,0,14.9)]
What is the source for this data? Is it a tab-delimited file? If so the CSV
module can help make this translation.
I also assume that you want the first 2 elements of a spot_int element to
match a spot_cor element.
Then (for the subset of data you've provided):
>>> for ele1 in spot_cor:
... for ele2 in spot_int:
... if ele1 == ele2[:2]:
... print "%8s %8s %8s" % ele2
...
10 0 37.3
>I want to write all the three columns of spot_int.
>[snip]
Hope that helps.
Bob Gailer
bgailer at alum.rpi.edu
303 442 2625 home
720 938 2625 cell
More information about the Tutor
mailing list