<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          非常有用的 Python 技巧

          共 3345字,需瀏覽 7分鐘

           ·

          2020-12-06 19:51

          點(diǎn)擊上方Python知識(shí)圈設(shè)為星標(biāo)

          回復(fù)100獲取100題PDF



          閱讀文本大概需要 5?分鐘


          近期精彩文章Python100例(附PDF下載地址)

          來(lái)源:蘇生不惑 ? 鏈接:

          https://mp.weixin.qq.com/s/ATvPzfLHwp0wEH5-tMW2cg

          函數(shù)連續(xù)調(diào)用


          def add(x):
          class AddNum(int):
          def __call__(self, x):
          return AddNum(self.numerator + x)
          return AddNum(x)

          print add(2)(3)(5)
          # 10
          print add(2)(3)(4)(5)(6)(7)
          # 27

          # javascript 版
          var add = function(x){
          var addNum = function(x){
          return add(addNum + x);
          };

          addNum.toString = function(){
          return x;
          }
          return addNum;
          }

          add(2)(3)(5)//10
          add(2)(3)(4)(5)(6)(7)//27

          默認(rèn)值陷阱


          >>> def evil(v=[]):
          ... v.append(1)
          ... print v
          ...
          >>> evil()
          [1]

          >>> evil()
          [1, 1]

          讀寫(xiě)csv文件


          import csv

          with open('data.csv', 'rb') as f:
          reader = csv.reader(f)
          for row in reader:
          print row

          # 向csv文件寫(xiě)入
          import csv

          with open( 'data.csv', 'wb') as f:
          writer = csv.writer(f)
          writer.writerow(['name', 'address', 'age']) # 單行寫(xiě)入
          data = [
          ( 'xiaoming ','china','10'),
          ( 'Lily', 'USA', '12')]

          writer.writerows(data) # 多行寫(xiě)入

          數(shù)制轉(zhuǎn)換


          >>> int('1000', 2)
          8

          >>> int('A', 16)
          10

          格式化 json

          echo'{"k": "v"}' | python-m json.tool

          list 扁平化


          list_ = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
          [k for i in list_ for k in i] #[1, 2, 3, 4, 5, 6, 7, 8, 9]
          import numpy as np
          print np.r_[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

          import itertools
          print list(itertools.chain(*[[1, 2, 3], [4, 5, 6], [7, 8, 9]]))
          sum(list_, [])
          flatten = lambda x: [y for l in x for y in flatten(l)] if type(x) is list else [x]
          flatten(list_)

          list 合并


          >>> a = [1, 3, 5, 7, 9]
          >>> b = [2, 3, 4, 5, 6]
          >>> c = [5, 6, 7, 8, 9]
          >>> list(set().union(a, b, c))
          [1, 2, 3, 4, 5, 6, 7, 8, 9]

          出現(xiàn)次數(shù)最多的 2 個(gè)字母


          from collections import Counter
          c = Counter('hello world')
          print(c.most_common(2)) #[('l', 3), ('o', 2)]

          謹(jǐn)慎使用

          eval("__import__('os').system('rm -rf /')", {})

          置換矩陣


          matrix = [[1, 2, 3],[4, 5, 6]]
          res = zip( *matrix ) # res = [(1, 4), (2, 5), (3, 6)]

          列表推導(dǎo)


          [item**2 for item in lst if item % 2]
          map(lambda item: item ** 2, filter(lambda item: item % 2, lst))
          >>> list(map(str, [1, 2, 3, 4, 5, 6, 7, 8, 9]))
          ['1', '2', '3', '4', '5', '6', '7', '8', '9']

          排列組合


          >>> for p in itertools.permutations([1, 2, 3, 4]):
          ... print ''.join(str(x) for x in p)
          ...
          1234
          1243
          1324
          1342
          1423
          1432
          2134
          2143
          2314
          2341
          2413
          2431
          3124
          3142
          3214
          3241
          3412
          3421
          4123
          4132
          4213
          4231
          4312
          4321

          >>> for c in itertools.combinations([1, 2, 3, 4, 5], 3):
          ... print ''.join(str(x) for x in c)
          ...
          123
          124
          125
          134
          135
          145
          234
          235
          245
          345
          >>> for c in itertools.combinations_with_replacement([1, 2, 3], 2):
          ... print ''.join(str(x) for x in c)
          ...
          11
          12
          13
          22
          23
          33
          >>> for p in itertools.product([1, 2, 3], [4, 5]):
          (1, 4)
          (1, 5)
          (2, 4)
          (2, 5)
          (3, 4)
          (3, 5)

          默認(rèn)字典


          >>> m = dict()
          >>> m['a']
          Traceback (most recent call last):
          File "", line 1, in
          KeyError: 'a'
          >>>
          >>> m = collections.defaultdict(int)
          >>> m['a']
          0
          >>> m['b']
          0
          >>> m = collections.defaultdict(str)
          >>> m['a']
          ''
          >>> m['b'] += 'a'
          >>> m['b']
          'a'
          >>> m = collections.defaultdict(lambda: '[default value]')
          >>> m['a']
          '[default value]'
          >>> m['b']
          '[default value]'

          反轉(zhuǎn)字典


          >>> m = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
          >>> m
          {'d': 4, 'a': 1, 'b': 2, 'c': 3}
          >>> {v: k for k, v in m.items()}
          {1: 'a', 2: 'b', 3: 'c', 4: 'd'}


          pk哥個(gè)人微信


          添加pk哥個(gè)人微信即送Python資料


          → Python知識(shí)點(diǎn)100題的PDF

          → Python相關(guān)的電子書(shū)10本


          記得備注:“100題”




          往期推薦
          01

          公眾號(hào)所有文章匯總導(dǎo)航(2-10更新)

          02

          Python100例(附PDF下載地址)

          03

          求你了,別再用 pip 那烏龜?shù)乃俣热グ惭b庫(kù)了!


          點(diǎn)擊閱讀原文查看pk哥原創(chuàng)視頻

          我就知道你“在看”

          瀏覽 41
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  西西4444www大胆艺术 | 果冻传媒91cm-084换妻下部董小宛 | 欧美成人A片Ⅴ一区二区三区动漫 | 婷五月丁香乱伦电影网站 | 52AV天堂 |