[Tutor] Please help matching elements from two lists and printing
them
kumar s
ps_python at yahoo.com
Wed Dec 8 22:51:53 CET 2004
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']
>>> 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']
I want to take each element from spot_cor and search
in spot_int, if they match, I want to write
all the three columns of spot_int.
I did the following way to see what happens when I
print element1 and element 2 as tab delim. text:
code:
>>> for ele1 in spot_cor:
for ele2 in spot_int:
if ele1 in ele2:
print (ele1+'\t'+ele2)
432 117 432 117 17.3
432 117 7 432 117.9
432 117 554 432 117.7
499 631 499 631 23.1
12 185 12 185 19.6
12 185 112 185 42.6
12 185 212 185 26.3
12 185 312 185 111.9
12 185 412 185 193.1
12 185 512 185 21.9
12 185 612 185 22.0
326 83 169 326 83.7
62 197 62 197 18.9
The problem with this script is that it is printing
all unwanted element of spot_int list. This is simply
crap for me. I want to print the columns only if first
two columns of both tables match.
The simple reason here I asked it to see if 12 and 185
are contained in two columns and pythons tells me, yes
they are present in 112 and 185 and this is a wrong
result.
Can you please suggest a better method for comparing
these two elements and then printing the third column.
thank you very much.
Cheers
K
__________________________________
Do you Yahoo!?
The all-new My Yahoo! - What will yours do?
http://my.yahoo.com
More information about the Tutor
mailing list