<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                #### 某隨機序列```list_x = [2, 3, 4, 6, 6, 7, 3, 2, 8, 3, 6]```中,找到出現次數最高的3個元素,他們出現的次數是多少? >1. 使用collections.Counter對象 >2. 將序列傳入Counter的構造器,得到Counter對象是元素頻度的字典,Counter.mist_common(n)方法得到頻度最高的n個元素的列表 ##### 使用方法 ```python from collections import Counter list_x = [2, 3, 4, 6, 6, 7, 3, 2, 8, 3, 6] c = Counter(list_x) # 得到出現頻度最高的3個元素 result = c.most_common(3) print(c) print(result) --------------------------------------------------------------- 輸出: Counter({3: 3, 6: 3, 2: 2, 4: 1, 7: 1, 8: 1}) [(3, 3), (6, 3), (2, 2)] ``` 解釋一下```print(c)```得到的是`Counter`對象:里面是一個字典類型,里面對應的關系是:列表```list_x```中的元素對應出現了幾次。```3```出現了`3`次,`6`出現了`3`次,`2`出現了`2`次,依次對應的關系。```{元素:出現的次數}``` 而,```c.most_common(3)```傳入的參數3的意思是:取出`Counter`中字典的前`3`個,也就是出現次數最大的前`3`個。不難看出,列表`list_x`中出現次數最多的也就是`3`、`6`和`2`,各出現了3次,3次,2次。 #### 對某英文文章的單詞,進行詞頻統計,找到出現次數最高的10個單詞,還要知道它們出現的次數又是多少。 就可以借用上面這個方法,還需要使用正則得到以單詞作為元素組成的列表。 ```pyhton import re from collections import Counter # 把text.txt里面的內容作為字符串讀取出來。 txt = open('test.txt').read() # 以非單詞字符分割 txt。就得出了一個很大的列表,列表中每個元素也就是一個個單詞。 c3 = Counter(re.split('\W+',txt)) # 找出出現頻次最高的10個元素 print(c3.most_common(10)) ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看