Home
 
 
Search:  
C C++ Perl PHP Python HTML ShellScripts
 
 
  Coding Books
  Tutorials
  Search Code
  Browse Code
  Link to Us
  Site News
  Contact Metalshell
 
 
 
  Submit Code   Statistics
 



Python Standard Library    (ISBN: 0596000960)


 

 List Price: $29.95
 Our Price: $20.97
 Used Price: $7.70

 Release Date: May, 2001
 Manufacturer: O'Reilly & Associates (Paperback)
 Sales Rank: 102,590

 Author: Fredrik Lundh









More Info

 Python Linked List 2002-09-29 17:47:02
  python 
Category: source:python:structures
Description: Example on using pythons list objects to sort, add, and remove strings from a data structure.
Platform: all
Author: mind
Viewed: 8412
Rating: 2.9/5 (41 votes)
If you have any questions about this piece of code or still need help, try posting your question on the forum.

 

Printable Version
linklist.py
#!/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
Rate this code:
(Not Helpful)  (Very Helpful) 

 
 
   Developer.*  
   Blue Parrots  
   Technipal  
   Defy Magazine  
   Code Project  
   Prog. Heaven  


Got Money?