<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基礎(chǔ)】Python 炫技操作:推導(dǎo)式的五種寫法

          共 6519字,需瀏覽 14分鐘

           ·

          2021-02-26 11:16

          作者:寫代碼的明哥

          來(lái)源:Python編程時(shí)光


          推導(dǎo)式(英文名:comprehensions),也叫解析式,是Python的一種獨(dú)有特性。

          推導(dǎo)式是可以從一個(gè)數(shù)據(jù)序列構(gòu)建另一個(gè)新的數(shù)據(jù)序列的結(jié)構(gòu)體。

          總共有四種推導(dǎo)式:

          1. 列表(list)推導(dǎo)式

          2. 字典(dict)推導(dǎo)式

          3. 集合(set)推導(dǎo)式

          4. 生成器推導(dǎo)式

          1. 列表推導(dǎo)式

          列表推導(dǎo)式的基本格式

          new_list = [expression for_loop_expression if condition]

          舉個(gè)例子。

          我想找出一個(gè)數(shù)值列表中為偶數(shù)的元素,并組成新列表,通常不用列表推導(dǎo)式,可以這么寫

          old_list = [0,1,2,3,4,5]

          new_list = []
          for item in old_list:
              if item % 2 == 0:
                  new_list.append(item)

          print(new_list) # output: [0, 2, 4]

          一個(gè)簡(jiǎn)單的功能,寫的代碼倒是不少。

          如果使用了列表推導(dǎo)式,那就簡(jiǎn)潔多了,而且代碼還變得更加易讀了。

          >>> old_list = [0,1,2,3,4,5]
          >>>
          >>> new_list = [item for item in old_list if item % 2 == 0]
          >>> print(new_list) # output: [0, 2, 4]
          [024]

          2. 字典推導(dǎo)式

          字典推導(dǎo)式的基本格式,和 列表推導(dǎo)式相似,只是把 [] 改成了 {},并且組成元素有兩個(gè):key 和 value,要用 key_expr: value_expr 表示。

          new_dict ={ key_expr: value_expr for_loop_expression if condition }

          舉個(gè)例子。

          我想從一個(gè)包含所有學(xué)生成績(jī)信息的字典中,找出數(shù)學(xué)考滿分的同學(xué)。

          old_student_score_info = {
              "Jack": {
                  "chinese"87,
                  "math"92,
                  "english"78
              },
              "Tom": {
                  "chinese"92,
                  "math"100,
                  "english"89
              }
          }

          new_student_score_info = {name: scores for name, scores in old_student_score_info.items() if scores["math"] == 100}
          print(new_student_score_info)
          # output: {'Tom': {'chinese': 92, 'math': 100, 'english': 89}}

          3. 集合推導(dǎo)式

          集合推導(dǎo)式跟列表推導(dǎo)式也是類似的。唯一的區(qū)別在于它使用大括號(hào){},組成元素也只要一個(gè)。

          基本格式

          new_set = { expr for_loop_expression if condition }

          舉個(gè)例子

          我想把一個(gè)數(shù)值列表里的數(shù)進(jìn)行去重處理

          >>> old_list = [0,0,0,1,2,3]
          >>>
          >>> new_set = {item for item in old_list}
          >>> print(new_set)
          {0123}

          4. 生成器推導(dǎo)式

          生成器推導(dǎo)式跟列表推導(dǎo)式,非常的像,只是把 [] 換成了 ()

          • 列表推導(dǎo)式:生成的是新的列表

          • 生成器推導(dǎo)式:生成的是一個(gè)生成器

          直接上案例了,找出一個(gè)數(shù)值列表中所有的偶數(shù)

          >>> old_list = [0,1,2,3,4,5]
          >>> new_list = (item for item in old_list if item % 2 == 0)
          >>> new_list
          <generator object <genexpr> at 0x10292df10>
          >>> next(new_list)
          0
          >>> next(new_list)
          2

          5. 嵌套推導(dǎo)式

          for 循環(huán)可以有兩層,甚至更多層,同樣的,上面所有的推導(dǎo)式,其實(shí)都可以寫成嵌套的多層推導(dǎo)式。

          但建議最多嵌套兩層,最多的話,代碼就會(huì)變得非常難以理解。

          舉個(gè)例子。

          我想打印一個(gè)乘法表,使用兩個(gè)for可以這樣寫

          for i in range(110):
              for j in range(1, i+1):
                  print('{}x{}={}\t'.format(j, i, i*j), end='')
              print("")

          輸出如下

          1x1=1    
          1x2=2    2x2=4   
          1x3=3    2x3=6   3x3=9   
          1x4=4    2x4=8   3x4=12  4x4=16  
          1x5=5    2x5=10  3x5=15  4x5=20  5x5=25  
          1x6=6    2x6=12  3x6=18  4x6=24  5x6=30  6x6=36  
          1x7=7    2x7=14  3x7=21  4x7=28  5x7=35  6x7=42  7x7=49  
          1x8=8    2x8=16  3x8=24  4x8=32  5x8=40  6x8=48  7x8=56  8x8=64  
          1x9=9    2x9=18  3x9=27  4x9=36  5x9=45  6x9=54  7x9=63  8x9=72  9x9=81

          如果使用嵌套的列表推導(dǎo)式,可以這么寫

          >>> print('\n'.join([' '.join(['%2d *%2d = %2d' % (col, row, col * row) for col in range(1, row + 1)]) for row in range(110)]))
           1 * 1 =  1
           1 * 2 =  2  2 * 2 =  4
           1 * 3 =  3  2 * 3 =  6  3 * 3 =  9
           1 * 4 =  4  2 * 4 =  8  3 * 4 = 12  4 * 4 = 16
           1 * 5 =  5  2 * 5 = 10  3 * 5 = 15  4 * 5 = 20  5 * 5 = 25
           1 * 6 =  6  2 * 6 = 12  3 * 6 = 18  4 * 6 = 24  5 * 6 = 30  6 * 6 = 36
           1 * 7 =  7  2 * 7 = 14  3 * 7 = 21  4 * 7 = 28  5 * 7 = 35  6 * 7 = 42  7 * 7 = 49
           1 * 8 =  8  2 * 8 = 16  3 * 8 = 24  4 * 8 = 32  5 * 8 = 40  6 * 8 = 48  7 * 8 = 56  8 * 8 = 64
           1 * 9 =  9  2 * 9 = 18  3 * 9 = 27  4 * 9 = 36  5 * 9 = 45  6 * 9 = 54  7 * 9 = 63  8 * 9 = 72  9 * 9 = 81



          往期精彩回顧





          本站qq群704220115,加入微信群請(qǐng)掃碼:

          瀏覽 25
          點(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>
                  成人免费激情视频 | 激情在线色婷婷 | 美女操逼视频网站 | 婷婷国产 | 大香蕉伊人电影网 |