LISTS Member Collect Comprehension python
<p>Feb 26, 2013 08:43 PM
LIST METHODS  from the tutorial)
animals = ['lion', 'tiger', 'ele', 'ele', 'ele', 'tiger']
animals.count('ele') =>  3
 
can also do list insertion  eg   animals.insert(2 , 'gir')
will put 'gir' before the 3rd item.
 
aniamls.append ('gir')  will append gir onto end of list.
 
.remove   removes the first instance
 
reverse  reverses the list
 
animals.sort   sorts it   also see STACKS and QUEUES.
 
 
Feb 09, 2013 06:46 PM
inventory = ['sword', 'armor', 'shield', 'healing potion']
 
if 'healing potion' in inventory:
    print "you will live to fight another day"
 
Jan 24, 2013 12:41 PM  * Jan 27 for something like LISP COLLECT/ list "comprehension" see below
 
comments are preceded by a hashtag.
to create a list, set it off by [] brackets and each item is sep by commas.
items can be numbers, strings, variables, etc.
note first item is the zeroeth item.
 
>>> zoo = ['lion','tiger', 'bear','giraffe']
>>> zoo
['lion', 'tiger', 'bear', 'giraffe']
 
>>> zoo[0:1]
['lion']
>>> zoo[0:3]
['lion', 'tiger', 'bear']
>>> zoo[1:4:2]
['tiger', 'giraffe']
 
>>> zoo
['lion', 'tiger', 'bear', 'giraffe']
>>> <b>zoo.append('deer') </b>  ...
>>> zoo
['lion', 'tiger', 'bear', 'giraffe', 'deer']
 
>>> pets = ['dog','cat', 'gerbil']
>>> pets
['dog', 'cat', 'gerbil']
>>> <b>mammals = zoo + pets</b>   ...
>>> mammals
['lion', 'tiger', 'bear', 'giraffe', 'deer', 'dog', 'cat', 'gerbil']
 
Jan 27, 2013 04:50 PM
 
TOPIC:  How to do equiv of this LISP expression  (for item in LIST1 collect (firstthing item))
 
>>> x=[['a','b'],['c','d'],['e','f']]    # or could enter this as  x=[('a','b'), ('c','d').... ] each of the
individual pairs is called a TUPLE in Python  (difft than a list).
>>> x
[['a', 'b'], ['c', 'd'], ['e', 'f']]
 
>>> basket=[]
>>> for element in x:
basket.append(element[0])
>>> basket
['a', 'c', 'e']
>>> <b>basket=[i[0] for i in x]   # this is called a "list comprehension"</b>
>>> basket
['a', 'c', 'e']
>>>
>>> basket=[i[0]+i[1] for i in x]
>>> basket
['ab', 'cd', 'ef']
 
 </p>