#!/usr/local/bin/python
#
# data structures example by mind@metalshell.com
#
# an example on data structures, this should go over
# just about every list object and data structure..
#
# 09/25/2002
#
# http://www.metalshell.com
#
# List Objects:
# append(x)
# - Add an item to the end of the list; equivalent to a[len(a):] = [x].
# extend(L)
# - Extend the list by appending all the items in the given list; equivalent
# - to a[len(a):] = L.
# insert(i, x)
# - Insert an item at a given position. The first argument is the index of
# - the element before which to insert, so a.insert(0, x) inserts at the
# - front of the list, and a.insert(len(a), x) is equivalent to a.append(x).
# remove(x)
# - Remove the first item from the list whose value is x. It is an error
# - if there is no such item.
# pop([i])
# - Remove the item at the given position in the list, and return it.
# - If no index is specified, a.pop() returns the last item in the list.
# - The item is also removed from the list.
# index(x)
# - Return the index in the list of the first item whose value is x.
# - It is an error if there is no such item.
# count(x)
# Return the number of times x appears in the list.
# sort()
# Sort the items of the list, in place.
# reverse()
# Reverse the elements of the list, in place.
# I'm not going to add comments before each example because the print's
# pretty much explain what each segment is doing, so if reading this
# example doesn't help please execute it and see how each list object
# works..
a = range(20)
print "Our number sequence before truncation:", a
print " <command done>: <results of command>"
print
a.append(999)
print "a.append(999):", a
print
a.extend('test')
print "a.extend('test'):", a
print
a.insert(0, 11)
print "a.insert(0, 11):", a
print " - If you didn't notice what this did it inserted '11' into the"
print " - first array of our sequence a[0], so look for 11 at the beginning"
print " - look below for 2 other examples"
print
b = len(a)
a.insert(len(a), 11)
print "a.insert(len(a), 11):", a
print " - our sequence length is", len(a), "which placed 11 at the very end"
print " - of our string sequence: a[%s]" %(b)
print
b = len(a) / 2
a.insert(len(a) / 2, 11)
print "a.insert(len(a) / 2, 11):", a
print " - in this statement we took the total length of our string sequence"
print " -", len(a), "and divided it by 2 which placed 11 at a[%s]" %(b)
print
a.remove(11)
print "a.remove(11):", a
print " - This statement removed the first 11 in our string sequence, which"
print " - should have removed 11 from a[0], assuming you haven't edited this"
print " - example and a.insert(0, 11) is still stated above.."
print
b = len(a) / 2
a.pop(len(a) / 2)
print "a.pop(len(a) / 2):", a
print " - This statement removed 12 from our string sequence, also assuming"
print " - this example hasn't been edited.. If you look close at the last"
print " - example you will see 11, 11, 12 and 12 is at position: a[%s]" %(b)
print " - which is half the length of our string sequence:", len(a)
print
a.index(5)
print "a.index(5):", a
print
b = a.count(11)
print "b = a.count(11):", b
print " - this statement shows 11 being found %s times" %(b)
print
a.sort()
print "a.sort():", a
print " - self explanatory"
print
a.reverse()
print "a.reverse():", a
print " - also self explanatory"
print
|