[Tutor] Finding duplicates entry in file

Ken G. beachkid at insightbb.com
Sat Mar 20 22:50:08 CET 2010


Thanks for the info.  I already adopted a program from another person 
and it works like a charm.   As for your question, I had no idea of if I 
had duplicate or more as there was some 570 line items.  I whittled it 
down to 370 line entries.  Whew.

Ken

Luke Paireepinart wrote:
>
>
> On Sat, Mar 20, 2010 at 11:34 AM, Ken G. <beachkid at insightbb.com 
> <mailto:beachkid at insightbb.com>> wrote:
>
>     What is a method I can use to find duplicated entry within a
>     sorted numeric file?  
>     I was trying to read a file reading two lines at once but
>     apparently, I can only read one line at a time.  Can the same file
>     be opened and read two times within a program?
>
>     For example, a file has:
>
>     1
>     2
>     2
>     3
>     4
>     4
>     5
>     6
>     6
>
>     The newly revised file should be:
>
>     1
>     2
>     3
>     4
>     5
>     6
>
>     Again, thanking the group for their input,
>
>
> One-liner:
> open("output.txt", "w").write("\n".join(sorted(set([i.strip() for i in 
> open("input.txt")]))))
>
>
> Just for fun :)
>
> also, in your algorithm, why are you assuming there are at most 1 
> extra entries in the case of a duplicate?  Why not generalize it for 
> all duplicates?
> -Luke

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100320/bc9cb139/attachment.html>


More information about the Tutor mailing list