|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
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. |
|
#3
|
|||
|
|||
|
Very good!
Thanks for the explanation, as well as the programming hint. I think I am getting less dumb each day. James |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Range() Anomaly ? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|