[Tutor] Finding duplicates entry in file
Luke Paireepinart
rabidpoobear at gmail.com
Sat Mar 20 22:02:48 CET 2010
On Sat, Mar 20, 2010 at 11:34 AM, Ken G. <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/c561055e/attachment.html>
More information about the Tutor
mailing list