sorting

Carel Fellinger cfelling at iae.nl
Thu Feb 28 11:16:10 EST 2002


Jeff McComas <jmackd1 at yahoo.com> wrote:
> As a newbie i find the python scripting language very interesting and useful.
> I am trying to write a script that will search a .txt file and have the script
> tell me how many of the same numbers are in the .txt file. I know you can do
> it with the f.count command but my problem is how can you do an input that

What is this f.count method you're talking about?

> makes the numbers strings? Or puts double quotes and a comma around a number?

I'm not sure I understand what you mean, but le'me try.

> This is what a raw input from a .txt file looks like:

> 34.6        25        78        89.2       45.7
> 23          92        47        20         85
> 56          78        23        49         92

Okee let's read this whole file in

   >>> text = open("some-file.txt").read()

And now let's split it up discarding all whitespace and new lines

   >>> number_strings = text.split()

We now have a list of strings, each string containing the decimal
representation of a number.  For fun let's turn those strings into
real numbers --below we use `list comprehension', it's believed to
make code readable once you understand what it does.  The square
brackets indicate we are creating a sequence, and that sequence
consists of things `x' turned into reals `real(x)'.  The things that
are thus turned into reals are all the strings `for x in' we've put in
the sequence `number_strings' above--

   >>> numbers = [ real(x) for x in number_strings ]

Now let's count them using a dictionary (count) to keep track of the
number of times we saw a specific number.  If you would like to treat
`2.0' as being different from `2', then we'll have to use the string
representations from the file (number_strings), otherwise we'd better
use the computer representations of those numbers (numbers)

   >>> count = {}
   >>> for x in numbers:  # or: for x in number_string:
   ...     try:
   ...          count[x] += 1   # increment the count for this number
   ...     except KeyError:     # or if that number is not yet in the dict
   ...          count[x] = 1
   ...

> out of that how can i pull out the numbers that are the same, say 78 = 2

So let's look this up --the % operator on strings let you fill bits
and pieces on marked places (%r and %d) in that string--

   >>> print "the number %r occured %d time(s)" % (78, count[78]))

or to get them all

   >>> for x in count:
   ...    print "the number %r occured %d time(s)" % (78, count[78]))
   ...
-- 
groetjes, carel



More information about the Python-list mailing list