problem about list indexing

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

    #1

    problem about list indexing

    Hi, there

    a = range(100)

    if I want to use No 7, 11, 56,90 in a, then the only way I do is [a[7],
    a[11], a[56], a[90]].
    Is there any other way?

    Thanks in advance.

  • John Machin

    #2
    Re: problem about list indexing

    hollowspook wrote:
    Hi, there
    >
    a = range(100)
    >
    if I want to use No 7, 11, 56,90 in a, then the only way I do is [a[7],
    a[11], a[56], a[90]].
    Is there any other way?
    >
    I presume a = range(100) is just an indication that a is a list -- the
    literal answer to your question as asked is simply [7, 11, 56, 90]

    In the general case that a is *any* list (or anything else that
    supports the index protocol, e.g. a string, a tuple, an array.array
    instance, or some instance of a so-written class), you can use a "list
    comprehension". Example:

    | >>a = 'qwertyuiopasdf ghjklzxcvbnm'
    | >>[a[x] for x in [25, 0, 13, 1]]
    | ['m', 'q', 'f', 'w']

    Do try and find "list comprehension" in the manual and in your book or
    tutorial. It's useful for much more than the above.

    HTH,
    John

    Comment

    • Steven D'Aprano

      #3
      Re: problem about list indexing

      On Sun, 26 Nov 2006 00:25:13 -0800, hollowspook wrote:
      Hi, there
      >
      a = range(100)
      >
      if I want to use No 7, 11, 56,90 in a, then the only way I do is [a[7],
      a[11], a[56], a[90]].
      Is there any other way?
      a = [7, 11, 56, 90]

      Are those numbers supposed to be in some sort of series? They aren't an
      arithmetic series:

      (11 - 7) = 4
      (56 - 11) = 45 # not a constant difference

      nor are they a geometric series:

      (11/7) = 1.57
      (56/11) = 5.09 # not a constant ratio

      They don't look like some form of a Fibonacci series:

      7+11 != 56
      11+56 != 90

      If they're just "random" numbers, plucked out of thin air, then you
      probably can't calculate them and you'll need to just create them in a
      list a = [7, 11, 56, 90].



      --
      Steven.

      Comment

      • hollowspook

        #4
        Re: problem about list indexing

        Thanks, John

        how about indexing 1-7, 10
        [range(1:8),10] will generate [[1, 2, 3, 4, 5, 6, 7], 10], instead of
        [1, 2, 3, 4, 5, 6, 7, 10]


        "John Machin дµÀ£º
        "
        hollowspook wrote:
        Hi, there

        a = range(100)

        if I want to use No 7, 11, 56,90 in a, then the only way I do is [a[7],
        a[11], a[56], a[90]].
        Is there any other way?
        >
        I presume a = range(100) is just an indication that a is a list -- the
        literal answer to your question as asked is simply [7, 11, 56, 90]
        >
        In the general case that a is *any* list (or anything else that
        supports the index protocol, e.g. a string, a tuple, an array.array
        instance, or some instance of a so-written class), you can use a "list
        comprehension". Example:
        >
        | >>a = 'qwertyuiopasdf ghjklzxcvbnm'
        | >>[a[x] for x in [25, 0, 13, 1]]
        | ['m', 'q', 'f', 'w']
        >
        Do try and find "list comprehension" in the manual and in your book or
        tutorial. It's useful for much more than the above.

        HTH,
        John

        Comment

        • bearophileHUGS@lycos.com

          #5
          Re: problem about list indexing

          hollowspook:
          how about indexing 1-7, 10
          [range(1:8),10] will generate [[1, 2, 3, 4, 5, 6, 7], 10], instead of
          [1, 2, 3, 4, 5, 6, 7, 10]
          (Note that range(1:8) is a syntax error).

          You can join and extend lists as you like:
          >>range(1, 8) + [10]
          [1, 2, 3, 4, 5, 6, 7, 10]

          See also the list.append and list.extend methods too.

          Bye,
          bearophile

          Comment

          • hollowspook

            #6
            Re: problem about list indexing

            Thanks, bearophile.
            range(1, 8) + [10] is great!

            "bearophileHUGS @lycos.com дµÀ£º
            "
            hollowspook:
            how about indexing 1-7, 10
            [range(1:8),10] will generate [[1, 2, 3, 4, 5, 6, 7], 10], instead of
            [1, 2, 3, 4, 5, 6, 7, 10]
            >
            (Note that range(1:8) is a syntax error).
            >
            You can join and extend lists as you like:
            >
            >range(1, 8) + [10]
            [1, 2, 3, 4, 5, 6, 7, 10]
            >
            See also the list.append and list.extend methods too.

            Bye,
            bearophile

            Comment

            • ZeD

              #7
              Re: problem about list indexing

              hollowspook wrote:
              how about indexing 1-7, 10
              [range(1:8),10] will generate [[1, 2, 3, 4, 5, 6, 7], 10], instead of
              [1, 2, 3, 4, 5, 6, 7, 10]
              >>range(1,8)+[10]
              [1, 2, 3, 4, 5, 6, 7, 10]

              --
              Under construction

              Comment

              • John Machin

                #8
                Re: problem about list indexing

                Steven D'Aprano wrote:
                On Sun, 26 Nov 2006 00:25:13 -0800, hollowspook wrote:
                >
                Hi, there

                a = range(100)

                if I want to use No 7, 11, 56,90 in a, then the only way I do is [a[7],
                a[11], a[56], a[90]].
                Is there any other way?
                >
                a = [7, 11, 56, 90]
                >
                Are those numbers supposed to be in some sort of series? They aren't an
                arithmetic series:
                >
                (11 - 7) = 4
                (56 - 11) = 45 # not a constant difference
                >
                nor are they a geometric series:
                >
                (11/7) = 1.57
                (56/11) = 5.09 # not a constant ratio
                >
                They don't look like some form of a Fibonacci series:
                >
                7+11 != 56
                11+56 != 90
                >
                If they're just "random" numbers, plucked out of thin air, then you
                probably can't calculate them and you'll need to just create them in a
                list a = [7, 11, 56, 90].
                >
                Actually, it's a cubic polynomial ;-)

                | >>def f(x):
                | ... return (
                | ... 7
                | ... + 4 * x
                | ... + 41 * x * (x - 1) // 2
                | ... - 52 * x * (x - 1) * (x - 2) // 6
                | ... )
                | ...
                | >>[f(x) for x in range(4)]
                | [7, 11, 56, 90]

                HTH,
                John

                Comment

                Working...