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

          12個高效Python小技巧,建議收藏!

          共 9164字,需瀏覽 19分鐘

           ·

          2023-07-15 22:15

          掌握以下12種快速學習編程的方法 也許下一個 Python 大神就是你

          01 交換變量
                    
                        a = 3
                      
                      
                        b = 6
                      
                    
          這個情況如果要交換變量在C++中,肯定需要一個空變量。 但是 Python 不需要,只需一行,大家看清楚了。
                    
                        a = 3
                      
                      
                        b = 6
                      
                      
                        a, b = b, a
                      
                      
                        print(a)
                      
                      
                        print(b)
                      
                    
          輸出為:
                    
                        >>> 6
                      
                      
                        >>> 3
                      
                    

          02 字典推導(Dictionary comprehensions) 和集合推導(Set comprehensions)
          大多數的熟悉 Python 的同學都知道且使用過列表推導(list comprehensions)。
          如果你對list comprehensions概念不是很熟悉——一個list comprehension就是一個更簡短、簡潔的創(chuàng)建一個list的方法。
                    
                        some_list = [1, 2, 3, 4, 5]
                      
                      
                        another_list = [ x + 1 for x in some_list ]
                      
                      
                        another_list
                      
                    
          輸出為:
                    
                        >>> [2, 3, 4, 5, 6]
                      
                    
          自從 Python3.1 起,我們可以用同樣的語法來創(chuàng)建集合和字典:
                    
                        
                          # 集合推導式
                        
                      
                      
                        some_list = [1, 2, 3, 4, 5, 2, 5, 1, 4, 8]
                      
                      
                        even_set = { x for x in some_list if x % 2 == 0 }
                      
                      
                        print(even_set)
                      
                    
          輸出為:
                    
                        >>> {8, 2, 4}
                      
                    

                    
                        
                          # 字典推導式
                        
                      
                      
                        d = { x: x % 2 == 0 for x in range(1, 11) }
                      
                      
                        print(d)
                      
                    
          輸出為:
                    
                        {1: False, 2: True, 3: False, 4: True, 5: False, 6: True, 7: False, 8: True, 9: False, 10: True}
                      
                    

          03 計數時使用Counter計數對象
          對于大多數同學來說,數一個東西是一項很常見的任務,而且在大多數情況下并不是很有挑戰(zhàn)性的事情——這里有幾種方法能更簡單的完成這種任務。
          Python 的collections類庫里有個內置的dict類的子類,是專門來干這種事情的:
                    
                        from collections import Counter
                      
                      
                        
                          
          # 統(tǒng)計字符串中元素出現的次數 c = Counter('Hello World, Hello Forchange') print(c)
          # 打印出現次數最多的三個元素 print(c.most_common(3))
          輸出為:
                    
                        >>> Counter({'l': 5, 'o': 4, 'e': 3, ' ': 3, 'H': 2, 'r': 2, 'W': 1, 'd': 1, ',': 1, 'F': 1, 'c': 1, 'h': 1, 'a': 1, 'n': 1, 'g': 1})
                      
                      
                        >>> [('l', 5), ('o', 4), ('e', 3)]
                      
                    

          04 if 語句在行內
                    
                        print ("Hello") if True else ("World")
                      
                    
          輸出為:
                    
                        >>> Hello
                      
                    

          05 數值比較
          這是小編見過諸多語言中很少有的簡便方法。
                    
                        x = 2
                      
                      
                        if 3 > x > 1:
                      
                      
                           print(x)
                      
                      
                        if 1 < x > 0:
                      
                      
                           print(x)
                      
                    
          輸出為:
                    
                        >>> 2
                      
                      
                        >>> 2
                      
                    

          06 同時迭代兩個列表
                    
                        west = ["湖人", "勇士"]
                      
                      
                        east = ["公牛", "凱爾特人"]
                      
                      
                        for teama, teamb in zip(west, east):
                      
                      
                             print( teama + " vs. " + teamb)
                      
                    
          輸出為:
                    
                        >>> 湖人 vs. 公牛
                      
                      
                        >>> 勇士 vs. 凱爾特人
                      
                    

          07 帶索引的列表迭代
                    
                        teams = ["湖人", "勇士", "公牛", "凱爾特人"]
                      
                      
                        for index, team in enumerate(teams):
                      
                      
                            print(index, team)
                      
                    
          輸出為:
                    
                        >>> 0 湖人>>> 1 勇士>>> 2 公牛>>> 3 凱爾特人
                      
                    

          08 初始化列表的值
                    
                        items = [0]*3
                      
                      
                        print(items)
                      
                    
          輸出為:
                    
                        >>> [0, 0, 0]
                      
                    

          09 列表轉換為字符串
                    
                        teams = ["湖人", "勇士", "公牛", "凱爾特人"]
                      
                      
                        print(','.join(teams))
                      
                    
          輸出為:
                    
                        >>> 湖人,勇士,公牛,凱爾特人
                      
                    

          10 從字典中獲取元素
          我承認try/except代碼并不雅致,不過這里有一種簡單方法,嘗試在字典中查找key,如果沒有找到對應的value將用第二個參數設為其變量值。

                    
                        data = {'user': 1, 'name': 'Max', 'three': 4}
                      
                      
                        try:
                      
                      
                           is_admin = data['admin']
                      
                      
                        except KeyError:
                      
                      
                           is_admin = False
                      
                    
          替換成這樣:
                    
                        data = {'user': 1, 'name': 'Max', 'three': 4}
                      
                      
                        is_admin = data.get('admin', False)
                      
                    

          11 獲取列表的子集
          有時,你只需要列表中的部分元素,這里是一些獲取列表子集的方法。

                    
                        x = [1,2,3,4,5,6]
                      
                      
                        
                          
          # 前3個 print x[:3]
          # 中間4個 print x[1:5]
          # 最后3個 print x[3:]
          # 奇數項 print x[::2]
          # 偶數項 print x[1::2]

          12 迭代工具

          和collections庫一樣,還有一個庫叫itertools,對某些問題能高效地解決。 其中一個用例是查找所有組合,他能告訴你在一個組中元素的所有可能的組合方式。

                    
                        from itertools import combinations
                      
                      
                        
                          
          teams = ["湖人", "勇士", "公牛", "凱爾特人"]
          for combo in combinations(teams, 2): print(combo)
          輸出為:
                    
                        >>> ('湖人', '勇士')
                      
                      
                        >>> ('湖人', '公牛')
                      
                      
                        >>> ('湖人', '凱爾特人')
                      
                      
                        >>> ('勇士', '公牛')
                      
                      
                        >>> ('勇士', '凱爾特人')
                      
                      
                        >>> ('公牛', '凱爾特人')
                      
                    
          END
          ?
          瀏覽 84
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

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

          手機掃一掃分享

          分享
          舉報
          <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>
                  亚洲免费成人版在线视频 | 12一14女人毛片 | 有码一区二区三区四区 | 国产尻屄视频 | 大香蕉xxxx |