[Tutor] yet another misunderstanding on my part

Clayton Kirkwood crk at godblessthe.us
Wed Oct 22 20:48:44 CEST 2014



!-----Original Message-----
!From: Tutor [mailto:tutor-bounces+crk=godblessthe.us at python.org] On
!Behalf Of Steven D'Aprano
!Sent: Wednesday, October 22, 2014 4:03 AM
!To: tutor at python.org
!Subject: Re: [Tutor] yet another misunderstanding on my part
!
!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.

Trust me, I iterate and look for examples instead of randomly running
around.
Regarding the index out of range, I know what it meant, I was just kind of
surprised that Python didn't automatically create the node and stuff a value
in.

!
!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 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

>>> freshfruit = ['  banana', '  loganberry ', 'passion fruit  ']
>>> [weapon.strip() for weapon in freshfruit]
['banana', 'loganberry', 'passion fruit']
>From 5.1.3 in the Python3 tutorial. Although not an = assignment, some value
is being stored in weapon after being strip of whitespace. I realize they
are different, maybe just not sure how different:<) In this case, somehow
Python seems to keep track of the location so that the modified
value(dropping spaces) replaces the original location.

!
!
!Try this instead:
!
!py> descriptions = []
!py> descriptions.append("something blue") descriptions.append("something
!py> round") 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)

So two questions remain. Why can't codes.append(code) just replace the code
in the previous line and descriptions.append(description) replace the
description in the previous line. Previous emails have seen something like
value.strip() = some value   . Second question, why can't a numeric index be
used to make assignment to a specific location like a[1] = "some value"? If
the mechanism is to use a.index(1,"some value"), it seems somewhat clumsy.
It is clear that once the array is created, it is possible to access the
value by numeric indexing such as:
        print( col_position, code[col_position], description[col_position])
which produces:
83 r7  Price / EPS Estimate Next Year
84 s7  Short Ratio

Also,
>>> # create a list of 2-tuples like (number, square)
>>> [(x, x**2) for x in range(6)]
[(0, 0), (1, 1), (2, 4), (3, 9), (4, 16), (5, 25)]

Why does this not need the .append or .insert? Square brackets around the
whole line?

Clayton


!
!
![...]
!> 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
!_______________________________________________
!Tutor maillist  -  Tutor at python.org
!To unsubscribe or change subscription options:
!https://mail.python.org/mailman/listinfo/tutor





More information about the Tutor mailing list