[Tutor] Please help matching elements from two lists and printing them

kumar s ps_python at yahoo.com
Thu Dec 9 04:52:38 CET 2004


Hi, 
 thank you very much for suggesting a way. 

In fact I tried and I found another way to do it.
could you please suggest if something is wrong because
I have false positive results in the output.  That
means I getting more that the values I have in
spot_cor. For example I have 2500 elements in spot_cor
list. I am searching each element if it is in
spot_init. IF it is there then I am writing it to a
file.  What I expect is to get 2500 elements. However
I am getting 500 elements extra. I do not understand
how is this possible. 

Code:

>>> out = open('sa_int_2.txt','w')
>>> for ele1 in range(len(spot_cor)):
	x = spot_cor[ele1]
	for ele2 in range(len(spot_int)):
		cols = split(spot_int[ele2],'\t')
		y = (cols[0]+'\t'+cols[1])
		if x == y:
			for ele3 in spot_int:
				if y in ele3:
					out.write(ele3)
					out.write('\n')






On top of this this process is VERY SLOW on high end
server too. I think its just the way it is to deal
with string processing. 



As you asked I am all parsing out the pieces for a
tab-delimitted text. I can get the values as CSV
instead of tab delimitted. But what is the way using
CSV to deal with this situation. 


thanks
Kumar



--- Bob Gailer <bgailer at alum.rpi.edu> wrote:

> 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 
> 
> 



		
__________________________________ 
Do you Yahoo!? 
Yahoo! Mail - 250MB free storage. Do more. Manage less. 
http://info.mail.yahoo.com/mail_250


More information about the Tutor mailing list