<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 技巧

          共 4112字,需瀏覽 9分鐘

           ·

          2021-05-20 17:59

          點擊上方小白學(xué)視覺”,選擇加"星標(biāo)"或“置頂

          重磅干貨,第一時間送達

          本文轉(zhuǎn)自:機器學(xué)習(xí)算法那些事


          函數(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]

          讀寫csv文件


          import csv

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

          # 向csv文件寫入
          import csv

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

          writer.writerows(data) # 多行寫入

          數(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 個字母

          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 "<stdin>", line 1, in <module>
          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'}

          更多


          1. https://www.zhihu.com/question/27376156

          2. stackoverflow.com/questions/101268/hidden-features-of-python

          3. https://zhuanlan.zhihu.com/p/22909144

          4. https://www.zhihu.com/question/29995881


          下載1:OpenCV-Contrib擴展模塊中文版教程
          在「小白學(xué)視覺」公眾號后臺回復(fù):擴展模塊中文教程即可下載全網(wǎng)第一份OpenCV擴展模塊教程中文版,涵蓋擴展模塊安裝、SFM算法、立體視覺、目標(biāo)跟蹤、生物視覺、超分辨率處理等二十多章內(nèi)容。

          下載2:Python視覺實戰(zhàn)項目52講
          小白學(xué)視覺公眾號后臺回復(fù):Python視覺實戰(zhàn)項目即可下載包括圖像分割、口罩檢測、車道線檢測、車輛計數(shù)、添加眼線、車牌識別、字符識別、情緒檢測、文本內(nèi)容提取、面部識別等31個視覺實戰(zhàn)項目,助力快速學(xué)校計算機視覺。

          下載3:OpenCV實戰(zhàn)項目20講
          小白學(xué)視覺公眾號后臺回復(fù):OpenCV實戰(zhàn)項目20講即可下載含有20個基于OpenCV實現(xiàn)20個實戰(zhàn)項目,實現(xiàn)OpenCV學(xué)習(xí)進階。

          交流群


          歡迎加入公眾號讀者群一起和同行交流,目前有SLAM、三維視覺、傳感器自動駕駛、計算攝影、檢測、分割、識別、醫(yī)學(xué)影像、GAN算法競賽等微信群(以后會逐漸細分),請掃描下面微信號加群,備注:”昵稱+學(xué)校/公司+研究方向“,例如:”張三 + 上海交大 + 視覺SLAM“。請按照格式備注,否則不予通過。添加成功后會根據(jù)研究方向邀請進入相關(guān)微信群。請勿在群內(nèi)發(fā)送廣告,否則會請出群,謝謝理解~


          瀏覽 35
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <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>
                  国产一级黄片 | 日韩无码破解 | 欧美精品乱码视频一二专区 | 国产老女人操逼 | 在线观看亚州 |