[Tutor] yet another misunderstanding on my part
Steven D'Aprano
steve at pearwood.info
Wed Oct 22 13:02:56 CEST 2014
On Tue, Oct 21, 2014 at 09:54:49PM -0700, Clayton Kirkwood wrote:
> col_position, code, description = 0, [], []
> key_name = raw_table.replace('\t','\n')
> for each_line in key_name.splitlines():
> if ':' in each_line:
> code[col_position], description.append() = each_line.split(':')
> #neither works; first one is out of range error, 2nd, can't assign to
> #function. I've used square brackets around various sections, and it doesn't
> like it
The trick to learning how to program is NOT to randomly make changes to
your code and hope that you get lucky, but to understand why you are
getting the errors you get.
Start here:
position = 0
codes = []
codes[position] = 999
This fails with an error:
IndexError: list assignment index out of range
What does that mean? It's an English sentence, a little bit terse but
still understandable:
"list assignment index" -- what's that? It's the index used inside the
square brackets, being used for list assignment. In this case, the index
is 0.
"out of range" -- what's that mean? It tells you that the index you have
provided (in this case, 0) is too big. Let's experiment to see what "too
big" means:
py> alist = ['a', 'b', 'c'] # three items
py> alist[0] = 'A'
py> alist[1] = 'B'
py> alist[2] = 'C'
py> alist[3] = 'D'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
Aha! You can only assign to items in a list which already exist. In the
experiment, I had a list with three items, item 0, item 1 and item 2,
and assigning to index 0, 1 and 2 succeeded. But assigning to item 3
(which doesn't exist) fails.
Go back to your list. It is an empty list, [], which means it has *no*
items in it. Since there are no items in it, you cannot assign to any
(non-existent) item.
If you can't assign to an item, since there aren't any, you have to
append to the list.
I'm not sure where you got the idea of writing
description.append() = something
from. There's nothing in Python that I've ever seen that suggests that
would work, and the error message should be clear:
py> x() = 23
File "<stdin>", line 1
SyntaxError: can't assign to function call
Try this instead:
py> descriptions = []
py> descriptions.append("something blue")
py> descriptions.append("something round")
py> descriptions.append("something flat")
py> print(descriptions)
['something blue', 'something round', 'something flat']
Like the name suggests, "append" appends something to the end of the
list. So your code, which started like this:
# doesn't work
col_position, code, description = 0, [], []
key_name = raw_table.replace('\t','\n')
for each_line in key_name.splitlines():
if ':' in each_line:
code[col_position], description.append() = each_line.split(':')
could be written something like this:
# note plural names
codes, descriptions = [], []
key_name = raw_table.replace('\t','\n')
for each_line in key_name.splitlines():
if ':' in each_line:
code, description = each_line.split(':')
codes.append(code)
descriptions.append(description)
[...]
> What am I not seeing? I *do* spend hours trying different options, and study
> various documentation which confuse me and don't seem consistent.
It seems to me that you are probably failing to isolate the problem to
*one* thing. When you get an error, you should ignore EVERYTHING else
and just play around with that one function until you understand it.
Open the interactive interpreter, and experiment, like I did above with
the "alist[0]" assignments.
--
Steven
More information about the Tutor
mailing list