[Tutor] OK here goes nothing [list manipulation]

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Mon, 14 Jan 2002 21:11:42 -0800 (PST)


Hi Don, welcome aboard!


> > day1 = input('Enter total hours on duty day1 ?')
> > day2 = input('Enter total hours on duty day2 ?')
> > day3 = input('Enter total hours on duty day3 ?')

[some code cut...]

> Clearly, asking for inputs like this is a bit tedious. You had a great
> idea with the historyList, so let's use it like so:
> 
> historyList = []
> for i in range(8):
>     querry_string = "Enter total hours on duty on day%d" %(i+1)
>     input = raw_input(querry_string)
>     historyList.append(input)


Pijus shows that Python lists are expandable --- we can append() elements
to an empty list, and Python will automagically expand the size of them
for us.  There's some reference material on Python lists here:

    http://www.python.org/doc/lib/typesseq-mutable.html

if we want to see a complete list of what lists can do.  Another good
source of information about lists is in the Python tutorial:

    http://www.python.org/doc/tut/node5.html#SECTION005140000000000000000
    http://www.python.org/doc/tut/node7.html#SECTION007100000000000000000

Lots of good stuff.  *grin* Play around with it a bit, and I think you'll
like what you see.



> print "-----------------------------------------------------"

There's an easier way to do this:

###
print '-' * 60
###

We can "multiply" strings with an integer, and it'll duplicate that string
that many times.  This is very convenient for building headers.  Here's an
example with the interpreter:

###
>>> header = '-=' * 20 + '-'
>>> header
'-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-'
###

For a quick overview of this, we can look at:

    http://www.python.org/doc/tut/node5.html#SECTION005120000000000000000



>  print "total hours on duty last 7 Days: " , historyList [-1]
>           + historyList[-2]+ historyList[-3]+ historyList[-4]
>           + historyList[-5]+ historyList[-6]+ historyList[-7]
> 
> Again, this is better handled by a little loop:
> 
> for i in historyList:
>     totalhours = totalhours + i
> print totalhours


It's possible to write helper functions that work with lists:

###
def sum(some_list):
    total = 0
    for item in some_list:
        total = total + item
    return total
###

so that, later on, when we add up historyList, it looks like a bulk
action:

###
totalhours = sum(historyList)
###

which I think is pretty nice.

If we just want to add a subset of this list, we can "slice" a portion of
the list off.  Here's an example of "slicing" in the Python interpreter:

###
>>> weekdays = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday']
>>> weekdays[0:3]
['monday', 'tuesday', 'wednesday']
>>> weekdays[-3:]
['wednesday', 'thursday', 'friday']
###

Combining these two ideas, getting the total hours of duty for the last
seven days can be short and sweet:

###
print "total hours on duty last 7 Days: ", sum(historyList[-7:])
###



> > # output list of hours from input
> >
> > print historyList [0:100]
> 
> If you want to do it like this, just say:
> 
> print historyList[:] # this gets you the whole list

Also,

###
print historyList
###

will work if we don't need to slice the list.


If you have questions, please feel free to ask.