list of lists

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

    list of lists

    Hi,

    I have a list that consists of lists.
    E.g. T=[[1, 2, 3], [4, 5], [6]]
    Is there a way to address the a specific component in the "inner" list
    directly?
    E.g. right now I want to get the second value of the first list.
    Unfortunately I have to save it to a variable first and then read it.
    a = T[0]
    print a[1]

    That kind of sucks, becaus I have to read a lot of values from a lot of
    lists! :-(
    Is there a faster way than my saving it to a "help variable" first?

    Thanks folks!!

    Regards, Tom

  • Zachary Beane

    #2
    Re: list of lists

    In article <bk4grj$me3$1@n ews.uni-kl.de>, Tom wrote:[color=blue]
    > Hi,
    >
    > I have a list that consists of lists.
    > E.g. T=[[1, 2, 3], [4, 5], [6]]
    > Is there a way to address the a specific component in the "inner" list
    > directly?
    > E.g. right now I want to get the second value of the first list.
    > Unfortunately I have to save it to a variable first and then read it.
    > a = T[0]
    > print a[1]
    >
    > That kind of sucks, becaus I have to read a lot of values from a lot of
    > lists! :-(
    > Is there a faster way than my saving it to a "help variable" first?[/color]

    To use your example:

    T=[[1, 2, 3], [4, 5], [6]]
    print T[0][1]

    Zach

    Comment

    • Johannes Gijsbers

      #3
      Re: list of lists

      On Mon, 15 Sep 2003 15:59:56 +0200, Tom wrote:
      [color=blue]
      > Hi,
      >
      > I have a list that consists of lists. E.g. T=[[1, 2, 3], [4, 5], [6]]
      > Is there a way to address the a specific component in the "inner" list
      > directly?
      > E.g. right now I want to get the second value of the first list.
      > Unfortunately I have to save it to a variable first and then read it. a =
      > T[0]
      > print a[1][/color]

      Try using T[0][1]. T[0] is exactly equivalent to 'a', so you can use
      the same operations, you don't have to assign to 'a' first.

      Johannes


      Comment

      • Peter Otten

        #4
        Re: list of lists

        Tom wrote:
        [color=blue]
        > Hi,
        >
        > I have a list that consists of lists.
        > E.g. T=[[1, 2, 3], [4, 5], [6]]
        > Is there a way to address the a specific component in the "inner" list
        > directly?
        > E.g. right now I want to get the second value of the first list.
        > Unfortunately I have to save it to a variable first and then read it.
        > a = T[0]
        > print a[1]
        >
        > That kind of sucks, becaus I have to read a lot of values from a lot of
        > lists! :-(
        > Is there a faster way than my saving it to a "help variable" first?
        >
        > Thanks folks!!
        >
        > Regards, Tom[/color]

        Your example rather than your problem specification suggests that you want
        them all. If I'm guessing right:

        def flatten(lol):
        for lst in lol:
        for item in lst:
        yield item

        T=[[1, 2, 3], [4, 5], [6]]

        for item in flatten(T):
        print item

        Indices seem to be a dying breed :-)

        Peter

        Comment

        • Paul Osman

          #5
          Re: list of lists

          On Mon, 15 Sep 2003, Tom wrote:
          [color=blue]
          > Hi,
          >
          > I have a list that consists of lists.
          > E.g. T=[[1, 2, 3], [4, 5], [6]]
          > Is there a way to address the a specific component in the "inner" list
          > directly?
          > E.g. right now I want to get the second value of the first list.
          > Unfortunately I have to save it to a variable first and then read it.
          > a = T[0]
          > print a[1]
          >
          > That kind of sucks, becaus I have to read a lot of values from a lot of
          > lists! :-(
          > Is there a faster way than my saving it to a "help variable" first?
          >
          > Thanks folks!!
          >
          > Regards, Tom
          >
          >[/color]

          Hi Tom,

          Do you mean this?
          [color=blue][color=green][color=darkred]
          >>> T=[[1, 2, 3], [4, 5], [6]]
          >>> T[0][1][/color][/color][/color]
          2[color=blue][color=green][color=darkred]
          >>> print T[0][1][/color][/color][/color]
          2[color=blue][color=green][color=darkred]
          >>> print T[1][1][/color][/color][/color]
          5

          HTH,

          --
          Paul Osman
          [email protected]


          "Idealists...fo olish enough to throw caution
          to the winds...have advanced mankind and have
          enriched the world."
          - Emma Goldman

          Comment

          • Lukasz Pankowski

            #6
            Re: list of lists


            Do you mean this:
            [color=blue][color=green][color=darkred]
            >>> T=[[1, 2, 3], [4, 5], [6]]
            >>> print T[0][1][/color][/color][/color]
            2

            --

            =*= Lukasz Pankowski =*=

            Comment

            • Daniel Klein

              #7
              Re: list of lists

              On Mon, 15 Sep 2003 15:59:56 +0200, Tom <[email protected] > wrote:
              [color=blue]
              >I have a list that consists of lists.
              >E.g. T=[[1, 2, 3], [4, 5], [6]]
              >Is there a way to address the a specific component in the "inner" list
              >directly?
              >E.g. right now I want to get the second value of the first list.
              >Unfortunatel y I have to save it to a variable first and then read it.
              >a = T[0]
              >print a[1]
              >
              >That kind of sucks, becaus I have to read a lot of values from a lot of
              >lists! :-(
              >Is there a faster way than my saving it to a "help variable" first?[/color]

              [color=blue][color=green][color=darkred]
              >>> T=[[1, 2, 3], [4, 5], [6]]
              >>> print t[0][1][/color][/color][/color]
              2

              Daniel Klein

              Comment

              • Tom

                #8
                Re: list of lists

                Hi guys,

                thanks to everyone.
                My mistake was that I had no idea that writing [][] NEXT to each other
                goes into depth. I always tried different varieties of [[]] to get into
                the deeper lists!
                Thanks again.

                CU Tom

                Comment

                • Dave Kuhlman

                  #9
                  Re: list of lists

                  Tom wrote:
                  [color=blue]
                  > Hi guys,
                  >
                  > thanks to everyone.
                  > My mistake was that I had no idea that writing [][] NEXT to each
                  > other goes into depth. I always tried different varieties of [[]]
                  > to get into the deeper lists!
                  > Thanks again.[/color]

                  It's a simple concept, once you grasp it, but for those new to
                  Python, it may be worth emphasizing -- You can concatenate
                  operators (to the right, at least) and these operators will
                  operate on the run-time value produced by the expression
                  to which they are applied. For example (read from bottom up):


                  getArray()[3].formatter()
                  ^ ^ ^ ^ ^
                  | | | | |
                  | | | | +--- (5) call function retrieved from
                  | | | | attribute
                  | | | +-------------- (4) access attribute of object indexed
                  | | | from array
                  | | +---------------- (3) index element of array returned by
                  | | function call
                  | +------------------- (2) call function retrieved from name
                  | binding
                  +------------------------ (1) retrieve value bound to variable


                  It is also worth thinking about what is meant by saying that this
                  evaluation is *dynamic*. For example, if the object returned by
                  the function call to getArray (above) is not indexable, then the []
                  operator will fail.

                  And, the only limiting factor is confusion.

                  Dave

                  --
                  Dave Kuhlman

                  [email protected] om

                  Comment

                  • Peter Otten

                    #10
                    Re: list of lists

                    Dave Kuhlman wrote:
                    [color=blue]
                    > It's a simple concept, once you grasp it, but for those new to
                    > Python, it may be worth emphasizing -- You can concatenate
                    > operators (to the right, at least) and these operators will
                    > operate on the run-time value produced by the expression
                    > to which they are applied. For example (read from bottom up):
                    >
                    >
                    > getArray()[3].formatter()
                    > ^ ^ ^ ^ ^
                    > | | | | |
                    > | | | | +--- (5) call function retrieved from
                    > | | | | attribute
                    > | | | +-------------- (4) access attribute of object indexed
                    > | | | from array
                    > | | +---------------- (3) index element of array returned by
                    > | | function call
                    > | +------------------- (2) call function retrieved from name
                    > | binding
                    > +------------------------ (1) retrieve value bound to variable
                    >
                    >
                    > It is also worth thinking about what is meant by saying that this
                    > evaluation is *dynamic*. For example, if the object returned by
                    > the function call to getArray (above) is not indexable, then the []
                    > operator will fail.
                    >
                    > And, the only limiting factor is confusion.[/color]

                    Nice explanation. I take the occasion to warn newbies that the sort() method
                    is a showstopper in this scheme:

                    getArray().sort ()[3].formatter() #BAD CODE, DON'T USE
                    ^
                    |
                    + -- sort() returns None

                    By the way, is there any Python programmer who has not made this error at
                    least once?

                    Peter

                    Comment

                    • Max M

                      #11
                      Re: list of lists

                      Peter Otten wrote:
                      [color=blue]
                      > Nice explanation. I take the occasion to warn newbies that the sort() method
                      > is a showstopper in this scheme:
                      >
                      > getArray().sort ()[3].formatter() #BAD CODE, DON'T USE
                      > ^
                      > |
                      > + -- sort() returns None
                      >
                      > By the way, is there any Python programmer who has not made this error at
                      > least once?[/color]


                      Also it is a slower approach if you want to look up more than one item
                      in the list. In that case it is better to bind the sorted result of
                      getArray to a variable.

                      arr = getArray()
                      arr.sort()
                      arr[3].formatter()
                      arr[4].formatter()
                      arr[5].formatter()


                      So I find that I rarely use the short form of the expression.

                      regards Max M

                      Comment

                      Working...