Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 28th, 2004, 03:09 AM
winxptipster winxptipster is offline
James
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: Beer Sheva, Israel
Posts: 12 winxptipster User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 27 m 27 sec
Reputation 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?

Reply With Quote
  #2  
Old May 28th, 2004, 04:00 AM
DevCoach DevCoach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2004
Location: London, England
Posts: 1,254 DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 8 h 9 m
Reputation Power: 265
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.

Reply With Quote
  #3  
Old May 28th, 2004, 05:22 AM
winxptipster winxptipster is offline
James
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: Beer Sheva, Israel
Posts: 12 winxptipster User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 27 m 27 sec
Reputation Power: 0
Very good!

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

I think I am getting less dumb each day.

James

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Range() Anomaly ?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT