Tuesday, October 27, 2015

list in python

http://effbot.org/zone/python-list.htm
https://developers.google.com/edu/python/lists?hl=en
https://docs.python.org/2/tutorial/datastructures.html
http://www.tutorialspoint.com/python/python_lists.htm

List range (Important)
https://deron.meranda.us/python/tutorial-bazaar/chapter2

s[ first:last ]. The last index is not included, but included if last idex is omitted such as s[first:]


Sublist:

L = [ "A", "B", [ "X", "Y" ], "C" ]
print L[-1]     --> "C"
print L[2]      --> ["X", "Y"]
print L[2][-1]  --> "Y"
print len(L)    --> 4


Sequence types: strings, lists, and tuples

A = B = [] # both names will point to the same list

A = []
B = A # both names will point to the same list

A = []; B = [] # independent lists

If you pass in a negative index, Python adds the length of the list to the index. L[-1] can be used to access the last item in a list.

a=[1,2,3]
a[-1] = a[3-1]

If you need both the index and the item, use the enumerate function:
    for index, item in enumerate(L):
        print index, item
The list object supports the iterator protocol. To explicitly create an iterator, use the built-in iter function:
    i = iter(L)
    item = i.next() # fetch first value
    item = i.next() # fetch second value

List Comprehension:
[x*x for x in [1,2,3]]



No comments:

Post a Comment