list Integer indexing dies??

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ishwor

    #1

    list Integer indexing dies??

    Hi all. Look at this snippet of code.
    [color=blue][color=green][color=darkred]
    >>> l = ['a','b','c','d']
    >>> l[/color][/color][/color]
    ['a', 'b', 'c', 'd'][color=blue][color=green][color=darkred]
    >>> l[0][0][0][/color][/color][/color]
    'a'
    It prints the value 'a'. Fine so far :-)
    l[0] ---> 'a' .
    l[0][0]---> 'a'[0] --> 'a'.
    l[0][0][0] ---> 'a'[0][0] --> 'a'[0] --> 'a'

    Now why doesnt this list which holds integer seem to work??
    [color=blue][color=green][color=darkred]
    >>> l = [1,2,3]
    >>> l[0][/color][/color][/color]
    1[color=blue][color=green][color=darkred]
    >>> l[0][0][/color][/color][/color]

    Traceback (most recent call last):
    File "<pyshell#244>" , line 1, in -toplevel-
    l[0][0]
    TypeError: unsubscriptable object[color=blue][color=green][color=darkred]
    >>> l[0][/color][/color][/color]
    1[color=blue][color=green][color=darkred]
    >>> 1[0][/color][/color][/color]

    Traceback (most recent call last):
    File "<pyshell#246>" , line 1, in -toplevel-
    1[0]
    TypeError: unsubscriptable object[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    The compiler reports unsubscriptable object ?? confused , dazzled i am ???!!??
    The same list now holds integer instead of strings and l[0][0][0]
    which worked fine earlier on strings doesn't seem to work on
    integers???
    Any help is greatly appreciated.



    --
    cheers,
    Ishwor Gurung
  • Antoon Pardon

    #2
    Re: list Integer indexing dies??

    Op 2004-12-23, Ishwor schreef <ishwor.gurung@ gmail.com>:[color=blue]
    > Hi all. Look at this snippet of code.
    >[color=green][color=darkred]
    >>>> l = ['a','b','c','d']
    >>>> l[/color][/color]
    > ['a', 'b', 'c', 'd'][color=green][color=darkred]
    >>>> l[0][0][0][/color][/color]
    > 'a'
    > It prints the value 'a'. Fine so far :-)
    > l[0] ---> 'a' .
    > l[0][0]---> 'a'[0] --> 'a'.
    > l[0][0][0] ---> 'a'[0][0] --> 'a'[0] --> 'a'
    >
    > Now why doesnt this list which holds integer seem to work??[/color]

    Because this only works with strings.

    String is the only object in python which has an implied
    equivallence between an element and a squence of one.

    So one character is a string and a string is a sequence
    of characters.

    So 'a'[0] is again 'a' which can again be indexed by
    0 as many times as you want.


    I think python would have been more consistent if
    strings would have been tuples of chars and maybe
    implied an equivalence between whatever object and
    a singleton of that object.

    --
    Antoon Pardon

    Comment

    • Ishwor

      #3
      Re: list Integer indexing dies??

      On 23 Dec 2004 14:28:37 GMT, Antoon Pardon <apardon@forel. vub.ac.be> wrote:[color=blue]
      > Op 2004-12-23, Ishwor schreef <ishwor.gurung@ gmail.com>:[color=green]
      > > Hi all. Look at this snippet of code.
      > >[color=darkred]
      > >>>> l = ['a','b','c','d']
      > >>>> l[/color]
      > > ['a', 'b', 'c', 'd'][color=darkred]
      > >>>> l[0][0][0][/color]
      > > 'a'
      > > It prints the value 'a'. Fine so far :-)
      > > l[0] ---> 'a' .
      > > l[0][0]---> 'a'[0] --> 'a'.
      > > l[0][0][0] ---> 'a'[0][0] --> 'a'[0] --> 'a'
      > >
      > > Now why doesnt this list which holds integer seem to work??[/color]
      >
      > Because this only works with strings.
      >
      > String is the only object in python which has an implied
      > equivallence between an element and a squence of one.
      >
      > So one character is a string and a string is a sequence
      > of characters.
      >
      > So 'a'[0] is again 'a' which can again be indexed by
      > 0 as many times as you want.[/color]

      ;-) gotcha. But shouldn't this be valid too??[color=blue][color=green][color=darkred]
      >>> 123232[0][/color][/color][/color]
      in which basically python can infer from the object type and print out
      1 instead of coughing up those errors? My experience as a learner here
      is that there should be some automagics & say like "okay you want to
      do indexing on integers ( context dependent); i'll give you the index
      of 0th position in that integer" ???


      [snip]

      Thanks Antoon.
      --
      cheers,
      Ishwor Gurung

      Comment

      • Antoon Pardon

        #4
        Re: list Integer indexing dies??

        Op 2004-12-23, Ishwor schreef <ishwor.gurung@ gmail.com>:[color=blue]
        > On 23 Dec 2004 14:28:37 GMT, Antoon Pardon <apardon@forel. vub.ac.be> wrote:[color=green]
        >> Op 2004-12-23, Ishwor schreef <ishwor.gurung@ gmail.com>:[color=darkred]
        >> > Hi all. Look at this snippet of code.
        >> >
        >> >>>> l = ['a','b','c','d']
        >> >>>> l
        >> > ['a', 'b', 'c', 'd']
        >> >>>> l[0][0][0]
        >> > 'a'
        >> > It prints the value 'a'. Fine so far :-)
        >> > l[0] ---> 'a' .
        >> > l[0][0]---> 'a'[0] --> 'a'.
        >> > l[0][0][0] ---> 'a'[0][0] --> 'a'[0] --> 'a'
        >> >
        >> > Now why doesnt this list which holds integer seem to work??[/color]
        >>
        >> Because this only works with strings.
        >>
        >> String is the only object in python which has an implied
        >> equivallence between an element and a squence of one.
        >>
        >> So one character is a string and a string is a sequence
        >> of characters.
        >>
        >> So 'a'[0] is again 'a' which can again be indexed by
        >> 0 as many times as you want.[/color]
        >
        > ;-) gotcha. But shouldn't this be valid too??[color=green][color=darkred]
        >>>> 123232[0][/color][/color][/color]

        Well if it should become valid, it should just return 123232 IMO.
        [color=blue]
        > in which basically python can infer from the object type and print out
        > 1 instead of coughing up those errors?[/color]

        Why do you feel it should cough up 1?

        Suppose I write a number in octal notation.

        What should 035[0] cough up? Be carefull it should
        cough up the same as 29[0].
        [color=blue]
        > My experience as a learner here
        > is that there should be some automagics & say like "okay you want to
        > do indexing on integers ( context dependent); i'll give you the index
        > of 0th position in that integer" ???[/color]

        Integers have no position. The position we think of in integers is an
        artefact of our notational system. Even then if we would simply defer
        to decimal notation there is still a problem. You see there are a
        number of arguments that would make 123232[0] cough up 2. Because
        by starting indexing from the back we get a nice correspondence between
        the index of the number and the power of 10 it represents.

        --
        Antoon Pardon

        Comment

        • Lonnie Princehouse

          #5
          Re: list Integer indexing dies??

          > ;-) gotcha. But shouldn't this be valid too??[color=blue][color=green][color=darkred]
          > >>> 123232[0][/color][/color][/color]

          No, it shouldn't be valid. It makes the implicit assumption that you
          want the base 10 digit, which isn't really a good assumption to make in
          the world of computers. Programmers are just as likely to want
          hexadecimal, and arguments could be made for octal or binary too.
          Basically, there's no intuitive meaning for trying to slice an integer.

          Also, it seems like it would be very rare that anybody would want to do
          this. Most of those cases fall into one of two categories- (1) trying
          to display digits of a number in a human readable way, and (2) trying
          to do some kind of math.

          (1) is better handled by explicitly converting it to a string first,
          and
          (2) is likely better handled using logarithms.

          Comment

          • Ishwor

            #6
            Re: list Integer indexing dies??

            On 23 Dec 2004 15:05:20 GMT, Antoon Pardon <apardon@forel. vub.ac.be> wrote:[color=blue]
            > Op 2004-12-23, Ishwor schreef <ishwor.gurung@ gmail.com>:[color=green]
            > > On 23 Dec 2004 14:28:37 GMT, Antoon Pardon <apardon@forel. vub.ac.be> wrote:[color=darkred]
            > >> Op 2004-12-23, Ishwor schreef <ishwor.gurung@ gmail.com>:
            > >> > Hi all. Look at this snippet of code.
            > >> >
            > >> >>>> l = ['a','b','c','d']
            > >> >>>> l
            > >> > ['a', 'b', 'c', 'd']
            > >> >>>> l[0][0][0]
            > >> > 'a'
            > >> > It prints the value 'a'. Fine so far :-)
            > >> > l[0] ---> 'a' .
            > >> > l[0][0]---> 'a'[0] --> 'a'.
            > >> > l[0][0][0] ---> 'a'[0][0] --> 'a'[0] --> 'a'
            > >> >
            > >> > Now why doesnt this list which holds integer seem to work??
            > >>
            > >> Because this only works with strings.
            > >>
            > >> String is the only object in python which has an implied
            > >> equivallence between an element and a squence of one.
            > >>
            > >> So one character is a string and a string is a sequence
            > >> of characters.
            > >>
            > >> So 'a'[0] is again 'a' which can again be indexed by
            > >> 0 as many times as you want.[/color]
            > >
            > > ;-) gotcha. But shouldn't this be valid too??[color=darkred]
            > >>>> 123232[0][/color][/color]
            >
            > Well if it should become valid, it should just return 123232 IMO.
            >[/color]
            Im not sure i understand u but what i meant was that[color=blue][color=green][color=darkred]
            >>> 123 + 2[/color][/color][/color]
            125 # nice n good

            now it would be nice if integer could also be *subscripted* too[color=blue][color=green][color=darkred]
            >>> 123[0] + 2[/color][/color][/color]
            3
            ;-) But as i said in earlier post said, i'll stick with import this's
            #2 by Tim Peters. Its better to leave these design issues with other
            **senior pythoneers**.

            [color=blue][color=green]
            > > in which basically python can infer from the object type and print out
            > > 1 instead of coughing up those errors?[/color]
            >
            > Why do you feel it should cough up 1?
            >[/color]

            [color=blue][color=green][color=darkred]
            >>>123232[0] #hypothetical 0th position in the integer.[/color][/color][/color]
            1

            [color=blue]
            > Suppose I write a number in octal notation.
            >
            > What should 035[0] cough up? Be carefull it should[/color]
            [color=blue][color=green][color=darkred]
            >>>035[0][/color][/color][/color]
            3 # my own opinion.
            [color=blue]
            > cough up the same as 29[0].[/color]
            [color=blue][color=green][color=darkred]
            >>>29[0][/color][/color][/color]
            2 #again my own opinion


            [snip]
            [color=blue]
            > by starting indexing from the back we get a nice correspondence between
            > the index of the number and the power of 10 it represents.
            >
            > --
            > Antoon Pardon
            > --
            > http://mail.python.org/mailman/listinfo/python-list
            >[/color]

            As in my view if python could treat object in context sensitive
            manner, it would be better world but its just my own beginners
            opinion.
            Happy hunting with Python. ;-)

            --
            cheers,
            Ishwor Gurung

            Comment

            • Stephen Thorne

              #7
              Re: list Integer indexing dies??

              On Fri, 24 Dec 2004 02:59:40 +1030, Ishwor <ishwor.gurung@ gmail.com> wrote:
              [color=blue][color=green][color=darkred]
              > >>> 123[0] + 2[/color][/color]
              > 3[/color]
              TypeError: unsubscriptable object
              [color=blue][color=green][color=darkred]
              > > > in which basically python can infer from the object type and print out
              > > > 1 instead of coughing up those errors?[/color]
              > >
              > > Why do you feel it should cough up 1?
              > >[/color]
              >[color=green][color=darkred]
              > >>>123232[0] #hypothetical 0th position in the integer.[/color][/color]
              > 1[/color]
              TypeError: unsubscriptable object
              [color=blue][color=green]
              > > Suppose I write a number in octal notation.
              > >
              > > What should 035[0] cough up? Be carefull it should[/color]
              >[color=green][color=darkred]
              > >>>035[0][/color][/color]
              > 3 # my own opinion.[/color]
              TypeError: unsubscriptable object
              [color=blue][color=green]
              > > cough up the same as 29[0].[/color]
              >[color=green][color=darkred]
              > >>>29[0][/color][/color]
              > 2 #again my own opinion[/color]
              TypeError: unsubscriptable object


              Just-in-my-own-opinion-ly y'rs

              Stephen Thorne

              Comment

              • Duncan Booth

                #8
                Re: list Integer indexing dies??

                Antoon Pardon wrote:
                [color=blue]
                > Suppose I write a number in octal notation.
                >
                > What should 035[0] cough up? Be carefull it should
                > cough up the same as 29[0].
                >[/color]

                given that 035==0000000000 000000000000000 000000000000000 00000035 I say they
                should both cough up 0 for any positive index of course.

                Comment

                • Mike Meyer

                  #9
                  Re: list Integer indexing dies??

                  Ishwor <ishwor.gurung@ gmail.com> writes:
                  [color=blue]
                  > On 23 Dec 2004 14:28:37 GMT, Antoon Pardon <apardon@forel. vub.ac.be> wrote:
                  > My experience as a learner here is that there should be some
                  > automagics & say like "okay you want to do indexing on integers (
                  > context dependent); i'll give you the index of 0th position in that
                  > integer" ???[/color]

                  Python doesn't do things automagically. The rules for pythonic
                  behavior include "Explicit is better than implicit" and "In the face
                  of ambiguity, refuse the tempation to guess." Both of those are
                  violated by automatic conversions and the like.

                  <mike
                  --
                  Mike Meyer <mwm@mired.or g> http://www.mired.org/home/mwm/
                  Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

                  Comment

                  • Ishwor

                    #10
                    Re: list Integer indexing dies??

                    On Fri, 24 Dec 2004 05:44:50 -0600, Mike Meyer <mwm@mired.or g> wrote:[color=blue]
                    > Ishwor <ishwor.gurung@ gmail.com> writes:
                    >[color=green]
                    > > On 23 Dec 2004 14:28:37 GMT, Antoon Pardon <apardon@forel. vub.ac.be> wrote:
                    > > My experience as a learner here is that there should be some
                    > > automagics & say like "okay you want to do indexing on integers (
                    > > context dependent); i'll give you the index of 0th position in that
                    > > integer" ???[/color]
                    >
                    > Python doesn't do things automagically. The rules for pythonic
                    > behavior include "Explicit is better than implicit" and "In the face
                    > of ambiguity, refuse the tempation to guess." Both of those are
                    > violated by automatic conversions and the like.
                    >[/color]

                    Well *shrugs* that was just an opionion though a bad one from a
                    beginner :-)... No hard feeling pythoner friends. ;-) and Merry
                    Christmas to you all ;-)
                    Then gingerbread is ready at

                    ;-)

                    [snip]

                    --
                    cheers,
                    Ishwor Gurung

                    Comment

                    • Mike Meyer

                      #11
                      Re: list Integer indexing dies??

                      Ishwor <ishwor.gurung@ gmail.com> writes:
                      [color=blue]
                      > On Fri, 24 Dec 2004 05:44:50 -0600, Mike Meyer <mwm@mired.or g> wrote:[color=green]
                      >> Ishwor <ishwor.gurung@ gmail.com> writes:
                      >>[color=darkred]
                      >> > On 23 Dec 2004 14:28:37 GMT, Antoon Pardon <apardon@forel. vub.ac.be> wrote:
                      >> > My experience as a learner here is that there should be some
                      >> > automagics & say like "okay you want to do indexing on integers (
                      >> > context dependent); i'll give you the index of 0th position in that
                      >> > integer" ???[/color]
                      >>
                      >> Python doesn't do things automagically. The rules for pythonic
                      >> behavior include "Explicit is better than implicit" and "In the face
                      >> of ambiguity, refuse the tempation to guess." Both of those are
                      >> violated by automatic conversions and the like.
                      >>[/color]
                      >
                      > Well *shrugs* that was just an opionion though a bad one from a
                      > beginner :-)... No hard feeling pythoner friends. ;-) and Merry
                      > Christmas to you all ;-)
                      > Then gingerbread is ready at
                      > http://www.mediatinker.com/blog/archives/008798.html[/color]

                      I'm aware that it was an opinion from a beginner. I thought it was
                      worth pointing out *why* this was a bad opinion in Python. I certainly
                      didn't intend to offend or insult anyone by doing so.

                      <mike
                      --
                      Mike Meyer <mwm@mired.or g> http://www.mired.org/home/mwm/
                      Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

                      Comment

                      Working...