help assigning list to variable
Author |
Message |
apython1992
![](http://compsci.ca/v3/uploads/user_avatars/17001640684d87a16bb8fbb.jpg)
|
Posted: Mon Jul 26, 2010 8:12 pm Post subject: help assigning list to variable |
|
|
I'm trying to process a data file for sorting purposes, but I'm not that far into it yet. Where I am having trouble is storing the partially modified data into a new variable. Here is the code:
# This program puts segments of satellite data in order by time and date.
import string
fileobject = open("c:\Users\Andrew\My Documents\space.txt", 'r')
lines = fileobject.readlines()
x = len(lines) - 1
y = () # Y is defined as an empty list
for i in range(0, x, 2):
list = string.split(lines[i], ',')
y[i] = list
print y #I have this line so that I can check the output and make sure it is what I'm going for...however,
#when I run the program, nothing happens. No syntax error.
Basically, what I'm doing in this loop is separating each line of string into a list, separated wherever a comma appears in the data file. The result is that each line that undergoes this modification becomes its own list, so that the entire file is a list, the elements of this larger list being the sublists (each line).
Since I want to continue to modify the file, I need to store this entire list in a new variable, as if I just left it under "list", The output at the end of what I have so far would be simply the last line in the data file, since the list is overwritten in each turn of the loop.
I have just started learning python today, so bear with me in that I do not know very much syntax at all. Any help is appreciated. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Draymire
|
Posted: Mon Jul 26, 2010 9:54 pm Post subject: Re: help assigning list to variable |
|
|
Zip up your program and the file you are using and upload it and then i will have a look, and please use code tags when you are posting code.
EDIT:
What version of python are you using |
|
|
|
|
![](images/spacer.gif) |
Nick
![](http://compsci.ca/v3/uploads/user_avatars/19644337547e837b41d67a.png)
|
Posted: Tue Jul 27, 2010 9:11 am Post subject: RE:help assigning list to variable |
|
|
look into the differences between lists and tuples, also look into the list.append method |
|
|
|
|
![](images/spacer.gif) |
CeciDai
|
Posted: Thu Dec 16, 2010 11:30 am Post subject: RE:help assigning list to variable |
|
|
You assigned y as a tuple, dumb, dumb
Go use a list
Like this
y=[]
to add stuff, use y.append(item)
BWHAHAHAHA |
|
|
|
|
![](images/spacer.gif) |
|
|