#1
  1. No Profile Picture
    James
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Location
    Beer Sheva, Israel
    Posts
    12
    Rep Power
    0

    Range() Anomaly ?


    Just curious about this.

    range(12)
    [0,1,2,3,4,5,6,7,8,9,10,11]

    (j,f,m,a,m,j,j,a,s,o,n,d)=range(12)

    j
    6

    j,j
    (6,6)

    j,j,j
    (6,6,6)

    Question: Why not "0" instead of 6?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    the line (j,f,m,a,m,j,j,a,s,o,n,d)=range(12) is assigning to j multiple times. It is equivalent to
    Code:
    j = 0
    f = 1
    m = 2
    a = 3
    m = 4
    j = 5
    j = 6
    etc...
    The earlier assignments to j are overwritten, so j will end up with the value of the last thing it is assigned to.

    What exactly do you want j to have? If you want it to contain a list of the values [0, 5, 6] then that can't be done directly. You can however create a dictionary with the letter as the key and a list of numbers as the values like this:

    Code:
    >>> months = {}
    >>> for index,month in enumerate('jfmamjjasond'): months.setdefault(month, []).append(index)
    ... 
    >>> months
    {'a': [3, 7], 'd': [11], 'f': [1], 'j': [0, 5, 6], 'm': [2, 4], 'o': [9], 'n': [10], 's': [8]}
    >>>
    (N.B. this requires Python 2.3 or later).

    Dave - The Developers' Coach
    Last edited by DevCoach; May 28th, 2004 at 04:08 AM.
  4. #3
  5. No Profile Picture
    James
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Location
    Beer Sheva, Israel
    Posts
    12
    Rep Power
    0
    Very good!

    Thanks for the explanation, as well as the programming hint.

    I think I am getting less dumb each day.

    James

IMN logo majestic logo threadwatch logo seochat tools logo