[Tutor] Comparing lines in two files, writing result into a third file

stuart_clemons@us.ibm.com stuart_clemons@us.ibm.com
Wed Apr 23 15:48:53 2003


This is a multipart message in MIME format.
--=_alternative 006CF50885256D11_=
Content-Type: text/plain; charset="US-ASCII"

Thanks Danny.   I appreciate the example.  I'll try to apply your example 
"merging" structure to my problem.  This is kind of similiar to the 
approach I was playing with.  I hope to have time to work on it tomorrow 
morning.  We'll see what I come up with.  Thanks again.
 




Danny Yoo <dyoo@hkn.eecs.berkeley.edu>
04/23/2003 03:32 PM
 
        To:     stuart_clemons@us.ibm.com
        cc:     "R. Alan Monroe" <amonroe@columbus.rr.com>, 
<tutor@python.org>
        Subject:        Re: [Tutor] Comparing lines in two files, writing 
result into a third file




On Wed, 23 Apr 2003 stuart_clemons@us.ibm.com wrote:

> So, anyway, now that I think about it a little bit, perhaps sorted order
> doesn't really matter.  One responder suggested that I use dictionaries
> in my code structure.  My understanding is that dictionaries are
> mappings, not sequences, so I guess ordering is not really relevant
> here.  FWIW, It does turn out that the files I'm working with are always
> ordered sequentially when I get them.
>
> Concerning dictionaries, do you think dictionaries is the structure to
> use ? If so, I'll try to spend some time reading up on dictionaries.  I
> do remember having problems reading a file into a dictionary when I
> tried it a year ago or so.

Hi Stuart,


Yes, dictionaries will work.

It's also very possible to do the problem without dictionaries if we use a
"merging" method.  I'll try describing the idea of the "merge"  method in
terms of cards and hands; maybe it will appeal to the card game players
here.  *grin*


Imagine that we have two lists in each of our hands:

###
left_hand = [1, 3, 5, 7, 9]
right_hand = [2, 4, 6, 8, 10]
###


And imagine that our task is to bring these two hands together into one
big pile, but make sure that the big pile is sorted.  If we do this in
real life, we'll notice that we'll do something like

    put down left card (1)
    put down right card (2)
    put down left card (3)
    put down right card (4)
    ...

and so we, in a sense, shuffle the cards into order.



Now say that our left and right hands hold something like this instead:

###
left_hand = [1, 2, 4, 6]
right_hand = [3, 5]
###

We'll still assume that each pile in our hands is individually sorted.
When we try to merge these lists together, our actions are similar, but we
do a little more to figure out which hand we should put down:

                                 left = [1, 2, 4, 6], right = [3, 5]
    put down left card (1)       left = [2,4,6],      right = [3, 5]
    put down left card (2)       left = [4, 6],       right = [3, 5]
    put down right card (3)      left = [4, 6],       right = [5]
    put down left card (4)       left = [6],          right = [5]
    put down right card (5)      left = [6],          right = []
    put down left card (6)       left = [],           right = []

If we do this by hand, we notice that we have to look at the top cards
from each hand, and do something based on what we see.  But what we do is
fairly simple:

###
### pseudocode
while we have cards in both left and right hands:
    if top left card < top right card:
        put_down top left card
    else:
        put_down top right card
put any remaining hand cards into large pile.
###

The pseudocode above isn't totally correct, because it doesn't take care
of the case when the top left card is the same as the top right card.
That's where you probably want to flag one of the the card with an
asterisk before putting it into the large pile, and discarding the other.


This should give some impression of the merging approach; you may need to
adjust it so that it works with files rather than left and right hands
though.  *grin*


Good luck!



--=_alternative 006CF50885256D11_=
Content-Type: text/html; charset="US-ASCII"


<br><font size=2 face="sans-serif">Thanks Danny. &nbsp; I appreciate the
example. &nbsp;I'll try to apply your example &quot;merging&quot; structure
to my problem. &nbsp;This is kind of similiar to the approach I was playing
with. &nbsp;I hope to have time to work on it tomorrow morning. &nbsp;We'll
see what I come up with. &nbsp;Thanks again.</font>
<br><font size=2 face="sans-serif">&nbsp;</font>
<br>
<br>
<br>
<table width=100%>
<tr valign=top>
<td>
<td><font size=1 face="sans-serif"><b>Danny Yoo &lt;dyoo@hkn.eecs.berkeley.edu&gt;</b></font>
<p><font size=1 face="sans-serif">04/23/2003 03:32 PM</font>
<td><font size=1 face="Arial">&nbsp; &nbsp; &nbsp; &nbsp; </font>
<br><font size=1 face="sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; To:
&nbsp; &nbsp; &nbsp; &nbsp;stuart_clemons@us.ibm.com</font>
<br><font size=1 face="sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; cc:
&nbsp; &nbsp; &nbsp; &nbsp;&quot;R. Alan Monroe&quot; &lt;amonroe@columbus.rr.com&gt;,
&lt;tutor@python.org&gt;</font>
<br><font size=1 face="sans-serif">&nbsp; &nbsp; &nbsp; &nbsp; Subject:
&nbsp; &nbsp; &nbsp; &nbsp;Re: [Tutor] Comparing lines in two files,
writing result into a third file</font></table>
<br>
<br>
<br><font size=2><tt><br>
<br>
On Wed, 23 Apr 2003 stuart_clemons@us.ibm.com wrote:<br>
<br>
&gt; So, anyway, now that I think about it a little bit, perhaps sorted
order<br>
&gt; doesn't really matter. &nbsp;One responder suggested that I use dictionaries<br>
&gt; in my code structure. &nbsp;My understanding is that dictionaries
are<br>
&gt; mappings, not sequences, so I guess ordering is not really relevant<br>
&gt; here. &nbsp;FWIW, It does turn out that the files I'm working with
are always<br>
&gt; ordered sequentially when I get them.<br>
&gt;<br>
&gt; Concerning dictionaries, do you think dictionaries is the structure
to<br>
&gt; use ? If so, I'll try to spend some time reading up on dictionaries.
&nbsp;I<br>
&gt; do remember having problems reading a file into a dictionary when
I<br>
&gt; tried it a year ago or so.<br>
<br>
Hi Stuart,<br>
<br>
<br>
Yes, dictionaries will work.<br>
<br>
It's also very possible to do the problem without dictionaries if we use
a<br>
&quot;merging&quot; method. &nbsp;I'll try describing the idea of the &quot;merge&quot;
&nbsp;method in<br>
terms of cards and hands; maybe it will appeal to the card game players<br>
here. &nbsp;*grin*<br>
<br>
<br>
Imagine that we have two lists in each of our hands:<br>
<br>
###<br>
left_hand = [1, 3, 5, 7, 9]<br>
right_hand = [2, 4, 6, 8, 10]<br>
###<br>
<br>
<br>
And imagine that our task is to bring these two hands together into one<br>
big pile, but make sure that the big pile is sorted. &nbsp;If we do this
in<br>
real life, we'll notice that we'll do something like<br>
<br>
 &nbsp; &nbsp;put down left card (1)<br>
 &nbsp; &nbsp;put down right card (2)<br>
 &nbsp; &nbsp;put down left card (3)<br>
 &nbsp; &nbsp;put down right card (4)<br>
 &nbsp; &nbsp;...<br>
<br>
and so we, in a sense, shuffle the cards into order.<br>
<br>
<br>
<br>
Now say that our left and right hands hold something like this instead:<br>
<br>
###<br>
left_hand = [1, 2, 4, 6]<br>
right_hand = [3, 5]<br>
###<br>
<br>
We'll still assume that each pile in our hands is individually sorted.<br>
When we try to merge these lists together, our actions are similar, but
we<br>
do a little more to figure out which hand we should put down:<br>
<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; left = [1, 2, 4, 6], right =
[3, 5]<br>
 &nbsp; &nbsp;put down left card (1) &nbsp; &nbsp; &nbsp; left = [2,4,6],
&nbsp; &nbsp; &nbsp;right = [3, 5]<br>
 &nbsp; &nbsp;put down left card (2) &nbsp; &nbsp; &nbsp; left = [4, 6],
&nbsp; &nbsp; &nbsp; right = [3, 5]<br>
 &nbsp; &nbsp;put down right card (3) &nbsp; &nbsp; &nbsp;left = [4, 6],
&nbsp; &nbsp; &nbsp; right = [5]<br>
 &nbsp; &nbsp;put down left card (4) &nbsp; &nbsp; &nbsp; left = [6], &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp;right = [5]<br>
 &nbsp; &nbsp;put down right card (5) &nbsp; &nbsp; &nbsp;left = [6], &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp;right = []<br>
 &nbsp; &nbsp;put down left card (6) &nbsp; &nbsp; &nbsp; left = [], &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; right = []<br>
<br>
If we do this by hand, we notice that we have to look at the top cards<br>
from each hand, and do something based on what we see. &nbsp;But what we
do is<br>
fairly simple:<br>
<br>
###<br>
### pseudocode<br>
while we have cards in both left and right hands:<br>
 &nbsp; &nbsp;if top left card &lt; top right card:<br>
 &nbsp; &nbsp; &nbsp; &nbsp;put_down top left card<br>
 &nbsp; &nbsp;else:<br>
 &nbsp; &nbsp; &nbsp; &nbsp;put_down top right card<br>
put any remaining hand cards into large pile.<br>
###<br>
<br>
The pseudocode above isn't totally correct, because it doesn't take care<br>
of the case when the top left card is the same as the top right card.<br>
That's where you probably want to flag one of the the card with an<br>
asterisk before putting it into the large pile, and discarding the other.<br>
<br>
<br>
This should give some impression of the merging approach; you may need
to<br>
adjust it so that it works with files rather than left and right hands<br>
though. &nbsp;*grin*<br>
<br>
<br>
Good luck!<br>
<br>
</tt></font>
<br>
--=_alternative 006CF50885256D11_=--