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, 09:13 AM
the_aimbot's Avatar
the_aimbot the_aimbot is offline
PHP ninja
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Melbourne, Australia
Posts: 20 the_aimbot User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Creating a list of tuples

I am trying to populate a list ot tuples on the fly from a file but keep running into these errors:
Code:
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "forum.py", line 84, in getPosts
    posts[0] =  [(details[0],(details[1],details[2],details[3]),post)]
IndexError: list assignment index out of range


Here is and simplified example of what i am trying to do:
Code:
 def getDetails(path, id):
    "Returns a list of user details in file "
    f = open(path + id + ".info", "r")
    index = 0
    posts = []
    line = f.readline()
    while line != "":
      line = f.readline()
      details = string.split(line[:-1], ",")
      posts[index] =  [(details[0],(details[1],details[2],details[3]),details[4])]
      index += 1
    f.close()
    return posts


Can somebody help I seem to be stuck

Reply With Quote
  #2  
Old May 28th, 2004, 10:11 AM
DevCoach DevCoach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2004
Location: London, England
Posts: 1,205 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 5 Days 17 h 39 m 53 sec
Reputation Power: 262
Unlike some languages, Python does not automatically extend lists when you assign values past the end. You need to use the append method instead, i.e.

Code:
posts.append([(details[0],(details[1],details[2],details[3]),details[4])])


BTW, if the original data is in CSV (comma separated variables) format then you could use the new csv module to parse it. This will correctly handle commas inside quoted strings.

Dave - The Developers' Coach

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Creating a list of tuples


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 2 hosted by Hostway