2015年9月8日 星期二

[RR Python] lambda function

a lambda function is a function that takes any number of arguments (including optional arguments)
and returns the value of a single expression. lambda functions can not contain commands, and
they can not contain more than one expression.

there are no parentheses around the argument list, and the return keyword is missing (it is implied, since the entire function can only be one expression).

>>>(lambda x: x*2)(3)
6

an other interesting lambda

Why do lambdas defined in a loop with different values all return the same result?



2015年9月3日 星期四

[RR Python] zip is not zip...(compression)

zip([iterable, ...])
This function returns a list of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables. The returned list is truncated in length to the length of the shortest argument sequence. When there are multiple arguments which are all of the same length, zip() is similar to map() with an initial argument of None. With a single sequence argument, it returns a list of 1-tuples. With no arguments, it returns an empty list.
The left-to-right evaluation order of the iterables is guaranteed. This makes possible an idiom for clustering a data series into n-length groups using zip(*[iter(s)]*n).
zip() in conjunction with the * operator can be used to unzip a list:


 an example:
alp = ['a','b','c','d','e']
num = range(5)

zan = zip(alp, num)
print type(zan), zan

ua, un = zip(*zan)
print ua
print un

and the result:


<type 'list'> [('a', 0), ('b', 1), ('c', 2), ('d', 3), ('e', 4)]
('a', 'b', 'c', 'd', 'e')
(0, 1, 2, 3, 4)

another example:


list_a = [3, 9, 17, 15, 19]
list_b = [2, 4, 8, 10, 30, 40, 50, 60, 70, 80, 90]
zab = zip(list_a, list_b)

print type(zab), zab

and the result:
<type 'list'> [(3, 2), (9, 4), (17, 8), (15, 10), (19, 30)]

apparently, the shorter one is turncated

[RR Python] enumerate

enumerate(sequence, start=0)
Return an enumerate object. sequence must be a sequence, an iterator, or some other object which supports iteration. The next() method of the iterator returned by enumerate() returns a tuple containing a count (from start which defaults to 0) and the values obtained from iterating over sequence:


vault = "abcdefg"
print type(enumerate(vault))
print list(enumerate(vault))
for index, x in enumerate(vault):
    print index, x
 result:
<type 'enumerate'>
[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd'), (4, 'e'), (5, 'f'), (6, 'g')]
0 a
1 b
2 c
3 d
4 e
5 f
6 g

[RR Python] else statement after while clause

As a C/C++ programmer, it's comfortable to use "while" to loop until certain expected condition happens in python, until I found that it's possible to have "else" after while loop.

there is good example at stackoverflow

and I tried this

vault = [1,2,3]

print "Start"

while vault:
    x = vault.pop(0)
    if x != None:
        print x
    else:
        print "BREAK"
        break
else:
    print "ELSE"
   
print "END"

and the result is
Start
1
2
3
ELSE
END

if change vault to [1,2,3,None]
vault = [1,2,3, None]

print "Start"

while vault:
    x = vault.pop(0)
    if x != None:
        print x
    else:
        print "BREAK"
        break
else:
    print "ELSE"
   
print "END"
the result is
Start
1
2
3
ELSE
END

The "else" can also apply to "for" loop. In short, if the loop test fails, "else" clause is executed, except exiting loop by "break"

Another useful example covers try-catch-else is at Else Clauses on Loop Statements

2015年8月25日 星期二

[RR Python] Unicode in Python

Get system encoding parameter:
sys.getdefaultencoding()
>>> sys.getdefaultencoding()
'ascii'
 Play around with multibyte characters
>>> msg = '今天天氣真好12345'
>>> msg
'\xe4\xbb\x8a\xe5\xa4\xa9\xe5\xa4\xa9\xe6\xb0\xa3\xe7\x9c\x9f\xe5\xa5\xbd12345'

>>> msgu = u'今天天氣真好12345'
>>> msgu
u'\u4eca\u5929\u5929\u6c23\u771f\u597d12345'
 >>> print msg, msgu
今天天氣真好12345 今天天氣真好12345
check their type
>>> print type(msg), type(msgu)
<type 'str'> <type 'unicode'>
 
the length of msg/msgu is interesting
 >>> print len(msg), len(msgu)
23 11
msg is encoded in "utf-8", to verify it, decode it and compare with msgu, they are identical!
>>> msg.decode('utf-8')
u'\u4eca\u5929\u5929\u6c23\u771f\u597d12345'


reference:
瞭解Unicode¶
Python Tutorial 第一堂(4)Unicode 支援、基本 I/O