Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #54142

Re: Help please, why doesn't it show the next input?

From John Gordon <gordon@panix.com>
Newsgroups comp.lang.python
Subject Re: Help please, why doesn't it show the next input?
Date 2013-09-13 22:41 +0000
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <l104ab$auc$1@reader1.panix.com> (permalink)
References <ef8de6db-5f35-4d07-8306-bcec47b1e69b@googlegroups.com> <75200616-79f5-4088-a967-d3a1381716f2@googlegroups.com> <mailman.304.1378978797.5461.python-list@python.org> <364bcdb3-fdd5-4774-b7d2-040e2ccb4cfd@googlegroups.com>

Show all headers | View raw


In <364bcdb3-fdd5-4774-b7d2-040e2ccb4cfd@googlegroups.com> William Bryant <gogobebe2@gmail.com> writes:

> Hello, I've done this so far but why doesn't the mode function work?

> def mode():
>     global NumberOfXItems, Themode
>     for i in List:
>         NumberOfXItems.append(i)
>         NumberOfXItems.append(List.count(i))
>     Themode = max(NumberOfXItems)
>     print(Themode)

As far as I can see, you're appending each of the user's numbers onto
NumberOfXItems, and you're also appending the number of times each number
occurs.

So, if the user had entered these numbers:

    5 9 9 9 15 100 100

NumberOfXItems would end up looking like this:

    5 1 9 3 9 3 9 3 15 1 100 2 100 2

The max is 100, but 9 is the most often-occuring number.

Also, since NumberOfXItems mixes user input and the counts of that input,
you risk getting a max that the user didn't even enter.  For example if the
user entered these numbers:

    1 1 1 1 1 1 2 3

NumberOfXItems would end up looking like this:

    1 6 1 6 1 6 1 6 1 6 1 6 2 1 3 1

The max is 6, which is a count, not user input.

mode would be much better written like this:

  def mode(mylist):

      max_occurrences = 0
      themode = None

      for i in mylist:
          thecount = mylist.count(i)
          if thecount > max_occurrences:
              max_occurrences = thecount
              themode = i

      print(themode)

-- 
John Gordon                   A is for Amy, who fell down the stairs
gordon@panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Help please, why doesn't it show the next input? William Bryant <gogobebe2@gmail.com> - 2013-09-10 21:49 -0700
  Re: Help please, why doesn't it show the next input? John Gordon <gordon@panix.com> - 2013-09-11 05:11 +0000
    Re: Help please, why doesn't it show the next input? William Bryant <gogobebe2@gmail.com> - 2013-09-10 22:39 -0700
      Re: Help please, why doesn't it show the next input? Jugurtha Hadjar <jugurtha.hadjar@gmail.com> - 2013-09-11 10:32 +0100
        Re: Help please, why doesn't it show the next input? William Bryant <gogobebe2@gmail.com> - 2013-09-11 12:33 -0700
          Re: Help please, why doesn't it show the next input? John Gordon <gordon@panix.com> - 2013-09-11 19:46 +0000
          Re: Help please, why doesn't it show the next input? Jugurtha Hadjar <jugurtha.hadjar@gmail.com> - 2013-09-12 02:14 +0100
          Re: Help please, why doesn't it show the next input? Jugurtha Hadjar <jugurtha.hadjar@gmail.com> - 2013-09-12 02:08 +0100
      Re: Help please, why doesn't it show the next input? Dave Angel <davea@davea.name> - 2013-09-11 11:50 +0000
        Re: Help please, why doesn't it show the next input? William Bryant <gogobebe2@gmail.com> - 2013-09-11 12:31 -0700
          Re: Help please, why doesn't it show the next input? Dave Angel <davea@davea.name> - 2013-09-11 20:32 +0000
          RE: Help please, why doesn't it show the next input? "Prasad, Ramit" <ramit.prasad@jpmorgan.com.dmarc.invalid> - 2013-09-11 19:58 +0000
          Re: Help please, why doesn't it show the next input? Terry Reedy <tjreedy@udel.edu> - 2013-09-11 17:14 -0400
  Re: Help please, why doesn't it show the next input? William Bryant <gogobebe2@gmail.com> - 2013-09-11 23:04 -0700
    Re: Help please, why doesn't it show the next input? Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-09-12 10:39 +0100
      Re: Help please, why doesn't it show the next input? William Bryant <gogobebe2@gmail.com> - 2013-09-13 15:12 -0700
        Re: Help please, why doesn't it show the next input? John Gordon <gordon@panix.com> - 2013-09-13 22:41 +0000
        Re: Help please, why doesn't it show the next input? MRAB <python@mrabarnett.plus.com> - 2013-09-13 23:48 +0100
          Re: Help please, why doesn't it show the next input? William Bryant <gogobebe2@gmail.com> - 2013-09-13 16:34 -0700

csiph-web