2014년 5월 19일 월요일

Python tips

1. change list of strings to list of arrays
   (1) use map(command, list) : apply ‘command’ to all elements of ‘list’
        -> in python 2: map returns list
        -> in python 3: map returns iterator


   (2) use list comprehension: [ ‘cmd’(x) for x in ‘list’]


2. search strings in lists
  (1) return index of matching list elements
   matching= [lines.index(s) for s in lines if "searching" in s]


3. join two arrays as columns
   np.column_stack(array1,array2)


4. multiple arguments as a tuple or unpack tuple
   *(tuple) unpacks the tuple
   **(dictionary) unpacks the dictionary
   it enables
   (1) defined function to receive arbitary number of arguments
         def f(*arguments)
    (2) passing arguments as tuple
         x=(1,2,3)
         f(*x) is equivalent f(1,2,3)

댓글 없음:

댓글 쓰기