Se ha denunciado esta presentación.
Super Advanced Python –act1 
https://www.spkrbar.com/talk/11 
參考Raymond Chandler III演講
Built-in Functions 
abs() divmod() input() open() staticmethod() 
all() enumerate() int() ord() str() 
any() eval() isinst...
Built-in Functions 
• 內建型態(Built-in type) 
– 數值型態(Numeric type) 
- int, long, float, bool, complex 
– 字串型態(String type) 
•...
三種基本type 
• list 型態 
• set 型態 
• dict 型態 
• tuple 型態
List comprehension 
• list = [1,2,3,4,5] 
{1: 10, 2: 20, 3: 30, 4: 40, 5: 50} 
{1: 10, 2: 20, 3: 30, 4: 40, 5: 50} 
[(1, 1...
Dict comprehension 
• my_dicts = {'a':1 ,'b':2, 'c':3} 
result = {k:v*2 for k, v in my_dicts.items()} 
print(result) 
prin...
Lambda 
• my_list = [1,2,3,4,5] 
def my_func(item): 
return item *2 
print([my_func(x) for x in my_list]) 
other_func = la...
Enumerate 
• my_first_list = ['a', 'b', 'c', 'd', 'e'] 
my_second_list = [1,2,3,4,5] 
print(zip(my_first_list, my_second_l...
zip 
• my_first_list = "abcde" 
my_second_list = "zyxwv" 
result = zip(my_first_list, my_second_list) 
print(result) 
resu...
filter 
• my_list = [1,2,3,4,5,6] 
print([x for x in my_list if x % 2 == 0]) 
print(filter(lambda x: x % 2 == 0, my_list))...
Any / all 
• my_list = [True,False,False,False] 
print(any(my_list)) 
print(all(my_list)) 
my_list2 = [True,True,True] 
pr...
• all(iterable)Return True if all elements of the iterable are true (or if the 
iterable is empty). Equivalent to: 
• def ...
map 
• my_list = range(1,7) 
#range(start, stop[, step]) 
print(my_list) 
print([x *2 for x in my_list]) 
range(): 
#range...
reduce 
• val = 0 
for x in range(1,7): 
val += x 
print(val) 
print(reduce(lambda x,y: x+y, range(1,7))) 
print(reduce(la...
參考網頁 
• http://www.codedata.com.tw/python/python-tutorial-the-2nd-class- 
2-container-flow-for-comprehension/ 
• http://54...
Próxima SlideShare
Cargando en…5
×

Super Advanced Python –act1

1.114 visualizaciones

Publicado el

2014.08.13 @NCU

Publicado en: Software
  • Sé el primero en comentar

Super Advanced Python –act1

  1. 1. Super Advanced Python –act1 https://www.spkrbar.com/talk/11 參考Raymond Chandler III演講
  2. 2. Built-in Functions abs() divmod() input() open() staticmethod() all() enumerate() int() ord() str() any() eval() isinstance() pow() sum() basestring() execfile() issubclass() print() super() bin() file() iter() property() tuple() bool() filter() len() range() type() bytearray() float() list() raw_input() unichr() callable() format() locals() reduce() unicode() chr() frozenset() long() reload() vars() classmethod() getattr() map() repr() xrange() cmp() globals() max() reversed() zip() compile() hasattr() memoryvie w() round() __import__() complex() hash() min() set() apply() delattr() help() next() setattr() buffer() dict() hex() object() slice() coerce() dir() id() oct() sorted() intern()
  3. 3. Built-in Functions • 內建型態(Built-in type) – 數值型態(Numeric type) - int, long, float, bool, complex – 字串型態(String type) • 補充format >>> '%(real)s is %(nick)s' % {'real' : 'Justin', 'nick' : 'caterpillar'} 'Justin is caterpillar‘ >>> '{0} is {1}'.format('Justin', 'caterpillar') 'Justin is caterpillar' – 容器型態(Container type) - list, set, dict, tuple
  4. 4. 三種基本type • list 型態 • set 型態 • dict 型態 • tuple 型態
  5. 5. List comprehension • list = [1,2,3,4,5] {1: 10, 2: 20, 3: 30, 4: 40, 5: 50} {1: 10, 2: 20, 3: 30, 4: 40, 5: 50} [(1, 10), (2, 20), (3, 30), (4, 40), (5, 50)] [(1, 100), (2, 200), (3, 300), (4, 400), (5, 500)] {1: 11, 2: 21, 3: 31, 4: 41, 5: 51} {'a': 2, 'c': 6, 'b': 4} print(dict([(v,v*10)for v in list])) print({v:v*10 for v in list}) my_dict = {v:v*10 for v in list} print(my_dict.items()) result = [(k,v*10) for (k,v) in my_dict.items()] print(result) dict_compr = {k:v+1 for k,v in my_dict.items()} print(dict_compr) # correct method my_dicts = {'a':1 ,'b':2, 'c':3} print({k:v*2 for k, v in my_dicts.items()}) Items(): #return (key, value) pairs 還有iterkeys(), itervalues(), iteritems()
  6. 6. Dict comprehension • my_dicts = {'a':1 ,'b':2, 'c':3} result = {k:v*2 for k, v in my_dicts.items()} print(result) print(result.iterkeys()) print(result.itervalues()) print(result.iteritems()) pairs1 = zip(result.iterkeys(),result.itervalues()) print(pairs1,type(pairs1)) pairs2 = [(v,k) for (k,v) in result.iteritems()] print(pairs2,type(pairs2)) {'a': 2, 'c': 6, 'b': 4} <dictionary-keyiterator object at 0x7f3bd764c940> <dictionary-valueiterator object at 0x7f3bd764c940> <dictionary-itemiterator object at 0x7f3bd764c940> ([('a', 2), ('c', 6), ('b', 4)], <type 'list'>) ([(2, 'a'), (6, 'c'), (4, 'b')], <type 'list'>)
  7. 7. Lambda • my_list = [1,2,3,4,5] def my_func(item): return item *2 print([my_func(x) for x in my_list]) other_func = lambda x: x*2 print([other_func(x) for x in my_list]) print(map(lambda i:i*2,my_list)) print(map(lambda i:i*i,my_list)) [2, 4, 6, 8, 10] [2, 4, 6, 8, 10] [2, 4, 6, 8, 10] [1, 4, 9, 16, 25]
  8. 8. Enumerate • my_first_list = ['a', 'b', 'c', 'd', 'e'] my_second_list = [1,2,3,4,5] print(zip(my_first_list, my_second_list)) print(enumerate(my_first_list)) print(enumerate(my_first_list, 3)) for i,j in enumerate(my_first_list,1): print(i,j) print([(i,j) for i,j in enumerate(my_first_list,1)]) [('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5)] <enumerate object at 0x7f3bd764d870> <enumerate object at 0x7f3bd764d870> (1, 'a') (2, 'b') (3, 'c') (4, 'd') (5, 'e') [(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e')]
  9. 9. zip • my_first_list = "abcde" my_second_list = "zyxwv" result = zip(my_first_list, my_second_list) print(result) result2 = [''.join(x) for x in result] print(result2) result3 = ['123'.join(x) for x in result] print(result3) print(dict(result)) print([(k*3,v) for k,v in result]) [('a', 'z'), ('b', 'y'), ('c', 'x'), ('d', 'w'), ('e', 'v')] ['az', 'by', 'cx', 'dw', 'ev'] ['a123z', 'b123y', 'c123x', 'd123w', 'e123v'] {'a': 'z', 'c': 'x', 'b': 'y', 'e': 'v', 'd': 'w'} [('aaa', 'z'), ('bbb', 'y'), ('ccc', 'x'), ('ddd', 'w'), ('eee', 'v')]
  10. 10. filter • my_list = [1,2,3,4,5,6] print([x for x in my_list if x % 2 == 0]) print(filter(lambda x: x % 2 == 0, my_list)) #filter(function, iterable) [2, 4, 6] [2, 4, 6]
  11. 11. Any / all • my_list = [True,False,False,False] print(any(my_list)) print(all(my_list)) my_list2 = [True,True,True] print(any(my_list2)) print(all(my_list2)) True False True True
  12. 12. • all(iterable)Return True if all elements of the iterable are true (or if the iterable is empty). Equivalent to: • def all(iterable): for element in iterable: if not element: return False return True • any(iterable)Return True if any element of the iterable is true. If the iterable is empty, return False. Equivalent to: • def any(iterable): for element in iterable: if element: return True return False
  13. 13. map • my_list = range(1,7) #range(start, stop[, step]) print(my_list) print([x *2 for x in my_list]) range(): #range(start, stop[, step]) print(map(lambda x:x *2 , my_list)) [1, 2, 3, 4, 5, 6] [2, 4, 6, 8, 10, 12] [2, 4, 6, 8, 10, 12]
  14. 14. reduce • val = 0 for x in range(1,7): val += x print(val) print(reduce(lambda x,y: x+y, range(1,7))) print(reduce(lambda x,y: x*y, range(1,7))) print(sum(range(1,7))) 21 21 720 21
  15. 15. 參考網頁 • http://www.codedata.com.tw/python/python-tutorial-the-2nd-class- 2-container-flow-for-comprehension/ • http://54im.com/python/%E3%80%90python-2-73-1- %E6%96%B0%E7%89%B9%E6%80%A7%E3%80%91%E5%AD%97%E 5%85%B8%E6%8E%A8%E5%AF%BC%E5%BC%8F%EF%BC%88dictio nary-comprehensions%EF%BC%89.html • https://docs.python.org/2/library/stdtypes.html?highlight=dict#dict .items • http://pydoing.blogspot.tw/2011/02/python-enumerate.html • http://pydoing.blogspot.tw/2011/03/python-strjoin.html • http://pydoing.blogspot.tw/2011/02/python-filter.html • https://docs.python.org/2/library/functions.html#all • https://docs.python.org/2/library/functions.html#reduce • https://www.spkrbar.com/talk/11

×