Lists

Some methods of list:

  • list.append(x): add x to the end
  • list.insert(i, x): insert x at position i
  • list.index(x): return index of the first item whose value is x
  • list.pop([i]): remove item at position i. default value is 0

In [29]:
pets = ['dog', 'cat', 'pig']
print pets.index('cat')


1

In [30]:
pets.insert(0, 'rabbit')
print pets


['rabbit', 'dog', 'cat', 'pig']

In [31]:
pets.pop(1)
print pets


['rabbit', 'cat', 'pig']
  • del statement can be used to remove an item from a list given its index

In [3]:
a = range(10)
print a


[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In [6]:
del a[2]
print a


[0, 1, 3, 4, 5, 6, 7, 8, 9]

In [5]:
print a[:3]


[0, 1, 2]

In [7]:
del a[:3]
print a


[4, 5, 6, 7, 8, 9]
  • list(): convert a sequence to a list

In [76]:
print list('i can eat glass')


['i', ' ', 'c', 'a', 'n', ' ', 'e', 'a', 't', ' ', 'g', 'l', 'a', 's', 's']

Sort a list

  • sorted(list, [cmp=None[, key=None[, reverse]]]): return a new sorted list
  • list.sort([cmp=None[, key=None[, reverse]]]): sort the current list (not creating new list)

where:

  • cmp: custom comparison function should return a negative, zero or positive number depending on whether the first argument is considered smaller than, equal to, or larger than the second argument
  • key: function extracting a comparison key from each list element.
  • reverse: if True, ascending sort otherwise, descending sort.

In [73]:
print sorted([2, 3, 1], reverse=True)
a = [2, 3, 1]
print a.sort(reverse=True)
print a


[3, 2, 1]
None
[3, 2, 1]

In [74]:
print sorted([
    ['peter', 23],
    ['john', 30],
    ['tom', 18]
], key=lambda x: x[1])


[['tom', 18], ['peter', 23], ['john', 30]]

List comprehensive

  • a concise way to create list
  • when need to create a list... think about it

In [38]:
squares = []
for x in range(10):
    squares.append(x**2)
print squares


[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

In [14]:
print [x**2 for x in range(10)]


[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

In [11]:
array = []
for x in [1,2,3]:
    for y in [1, 2, 3]:
        if x != y:
            array.append((x, y))
print array


[(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)]

In [12]:
print [(x, y) for x in [1,2,3] for y in [1,2,3] if x != y]


[(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)]

Tuples

  • lists and strings are two examples of sequence data types.
  • tuple can think like list, but it is immutable.
    • Note: no tuple comprehensive

In [16]:
t = (1, 2, 3, 4, 5)
print t


(1, 2, 3, 4, 5)

In [21]:
tuple([1,2,3])


Out[21]:
(1, 2, 3)

In [43]:
# change the tuple raise exception
t[0] = 5


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-43-db18f5e48428> in <module>()
      1 # change the tuple raise exception
----> 2 t[0] = 5

TypeError: 'tuple' object does not support item assignment

Sets

  • A set is an unordered collection with no duplicate elements
  • Curly brackets can use to create set, but {} create dictionary instead of set
  • set(): convert a sequence to a set

In [22]:
letters = {'a', 'b', 'c', 'a'}
print letters


set(['a', 'c', 'b'])

In [23]:
print set(['a', 'b', 'c', 'a'])


set(['a', 'c', 'b'])

In [23]:
s = set(['a', 'b'])
s.add('c')

print s


set(['a', 'c', 'b'])
  • add item to a set

In [104]:
pets = { 'dog', 'cat', 'pig' }
pets.add('dog')
print pets
pets.add('fish')
print pets


set(['cat', 'dog', 'pig'])
set(['fish', 'cat', 'dog', 'pig'])
  • check item is in a set

In [105]:
print 'fish' in pets
print 'lion' in pets


True
False
  • delete item

In [106]:
pets.remove('fish')
print pets


set(['cat', 'dog', 'pig'])
  • similarly to list comprehensions, set comprehensions are also supported

In [24]:
letters = {x for x in 'i can eat glass'}
print letters


set(['a', ' ', 'c', 'e', 'g', 'i', 'l', 'n', 's', 't'])
  • you can loop over the set

In [75]:
for c in set('i can eat glass'):
    print c,


a   c e g i l n s t

Dictionaries

  • it's mapping type: key -> value
  • key can be any immutable type: usually string or number

In [ ]:
{'a', 'b'}

In [26]:
tel = {'jack': 4098, 'sape': 4139}
tel['guido'] = 4127

print tel


{'sape': 4139, 'jack': 4098, 'guido': 4127}

In [27]:
tel['vu'] = 4910

In [28]:
print tel


{'vu': 4910, 'sape': 4139, 'jack': 4098, 'guido': 4127}

In [26]:
print tel['jack']


4098
  • delete key in dictionary

In [92]:
del tel['guido']
print tel


{'sape': 4139, 'jack': 4098}
  • check if a key is in dictionary

In [29]:
print 'sape' in tel
print 'foo' in tel


True
False
  • dict.keys(): return list of keys
  • dict.values(): return list of values

In [30]:
tel = {'sape': 4139, 'jack': 4098, 'guido': 4127}

print tel.keys()
print tel.values()


['sape', 'jack', 'guido']
[4139, 4098, 4127]
  • dictionary can be construct by calling
    • dict(sequence): where a sequence of (key, value) pairs

In [31]:
print dict([('sape', 4139), ('jack', 4098), ('guido', 4127)])


{'sape': 4139, 'jack': 4098, 'guido': 4127}
  • zip(sequence...): zip sequences together

In [115]:
zip([1, 2, 3], 'abc', 'ABC')


Out[115]:
[(1, 'a', 'A'), (2, 'b', 'B'), (3, 'c', 'C')]

In [116]:
print dict(zip('abc', [1, 2, 3]))


{'a': 1, 'c': 3, 'b': 2}

Loop over a dictionary


In [83]:
for name in tel:
    print name, ':', tel[name]


sape : 4139
jack : 4098
guido : 4127

In [32]:
tel.values()


Out[32]:
[4139, 4098, 4127]

In [81]:
for telno in tel.values():
    print telno


4139
4098
4127

Generator & iterator

Generator

  • generator: for lazy (on demand) generating of sequence of values
  • performance benefit!

In [34]:
def firstn(n):
    i = 0
    while i < n:
        yield i
        i += 1

In [35]:
gen = firstn(10)

In [42]:
print range(50)
print firstn(50)


[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49]
<generator object firstn at 0x10adad2d0>

In [58]:
for i in range(5):
    print i,
    
print '\n--------------------'

for i in firstn(5):
    print i,


0 1 2 3 4 
--------------------
0 1 2 3 4
  • comprehensive syntax like list comprehensive

In [62]:
for i in (x ** 2 for x in range(10)):
    print i,


0 1 4 9 16 25 36 49 64 81

Iterator

  • iterator: is an object that enables a program to traverse though a sequence
  • for statement: loop through any iterator

In [45]:
for i in xrange(10):
    print i,


0 1 2 3 4 5 6 7 8 9

More on looping

  • enumerate(sequence, start=0)

In [47]:
list(enumerate(['dog', 'cat', 'pig']))


Out[47]:
[(0, 'dog'), (1, 'cat'), (2, 'pig')]

In [89]:
print list(enumerate(['dog', 'cat', 'pig']))
print list(enumerate(['dog', 'cat', 'pig'], start=2))


[(0, 'dog'), (1, 'cat'), (2, 'pig')]
[(2, 'dog'), (3, 'cat'), (4, 'pig')]

In [48]:
for value in enumerate(['dog', 'cat', 'pig']):
    print value


(0, 'dog')
(1, 'cat')
(2, 'pig')

In [91]:
for index, value in enumerate(['dog', 'cat', 'pig']):
    print index, ':', value


0 : dog
1 : cat
2 : pig
  • dict.iteritems(): return an iterator through items (key, value) of a dictionary
  • dict.iterkeys(): return an iterator through key of a dictionary
  • dict.itervalues(): return an iterator through value of a dictionary

In [52]:
print tel


{'sape': 4139, 'jack': 4098, 'guido': 4127}

In [53]:
print list(tel.iteritems())


[('sape', 4139), ('jack', 4098), ('guido', 4127)]

In [54]:
for name, telno in tel.iteritems():
    print name, ':', telno


sape : 4139
jack : 4098
guido : 4127

In [56]:
for key in tel.iterkeys():
    print key


sape
jack
guido

In [57]:
import os

In [65]:
os.listdir('.')


Out[65]:
['.DS_Store',
 '.git',
 '.gitignore',
 '.ipynb_checkpoints',
 '0.0_Introduction.ipynb',
 '0.0_Introduction.slides.html',
 '1.0_Control_Flows.html',
 '1.0_Control_Flows.ipynb',
 '1.0_Control_Flows.slides.html',
 '2.0_Functions.ipynb',
 '2.0_Functions.slides.html',
 '3.0_Data_structures.ipynb',
 '3.0_Data_structures.slides.html',
 '4.0_Modules.ipynb',
 '4.0_Modules.slides.html',
 '5.0_Practices.ipynb',
 'assets',
 'examples',
 'LICENSE',
 'main.py',
 'module_a.py',
 'module_b.py',
 'README.md']

In [59]:
[file_name for file_name in os.listdir('.') if file_name.endswith('.pyc')]


Out[59]:
['main.pyc', 'module_a.pyc', 'module_b.pyc']

In [64]:
filter(lambda file_name: file_name.endswith('.pyc'), os.listdir('.'))


Out[64]:
[]

In [61]:
os.remove('./main.pyc')

In [63]:
[os.remove(file_name) for file_name in os.listdir('.') if file_name.endswith('.pyc')]


Out[63]:
[None, None]

In [ ]: