[Tutor] yet another misunderstanding on my part

Dave Angel davea at davea.name
Thu Oct 23 10:52:48 CEST 2014


"Clayton Kirkwood" <crk at godblessthe.us> Wrote in message:


(Somehow,  your email program seems to be using the exclamation
 point to identify quoted lines,  instead of the standard
 greater-than symbol. Is that something you can correct,  prrhaps
 using  "settings"?)

> 
> 
> !-----Original Message-----
> !From: Tutor [mailto:tutor-bounces+crk=godblessthe.us at python.org] On
> !Behalf Of Steven D'Aprano

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

That last line won't work either.

If you did try to do it on one line, you'd need to put the
 argument to append inside the parens, not on the other side of an
 equal sign. Something like  (untested )

  codes.append (each_line.split()[0])
  descriptions.append  (each_line.split()[1]

 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"),

The index() method does not change the object a, at least not for
 list objects. So that is not a replacement for subscripted
 assignment. 

The form a[1] = "some_value"  works fine, as long as there is
 already an object in that list element. In other words, it can be
 used to replace an item, but not to change the size. Python does
 not support sparse lists, so rather than permitting a size
 increase of exactly one, it was decided that the syntax would not
 permit any size change. And to grow by 1, append works very
 well.

If you know ahead of time what size you need, you could prefill it
 with something like:

   a = [] * 20

But you might want to check later that you replaced them all. 

> it seems somewhat clumsy.
> It is clear that once the array

We have been talking about lists here, not arrays. The type
 array.array behaves similarly, but not the same.

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

Those square brackets, with the for keyword inside, comprise a
 more advanced technique called a list comprehension. It's
 syntactic sugar for a for loop with an append. But the list
 object being built is anonymous, so it can frequently be used
 inside other expressions. 

I recommend ignoring list comprehensions till you can reliably
 write the loop.

You seem determined to cram things onto a single line that would
 probably be more readable as separate ones. And intermediate
 variables give an opportunity to make up useful names,  to make
 the code easier to read.


-- 
DaveA



More information about the Tutor mailing list