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

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Python 程序:計算每個元音的數量 > 原文: [https://www.programiz.com/python-programming/examples/count-vowel](https://www.programiz.com/python-programming/examples/count-vowel) #### 在此程序中,您將學習使用字典和列表推導式來計算字符串中每個元音的數量。 要理解此示例,您應該了解以下 [Python 編程](/python-programming "Python tutorial")主題: * [Python `for`循環](/python-programming/for-loop) * [Python 字符串](/python-programming/string) * [Python 字符串方法](/python-programming/strings-method) * * * ## 源代碼:使用字典 ```py # Program to count the number of each vowels # string of vowels vowels = 'aeiou' ip_str = 'Hello, have you tried our tutorial section yet?' # make it suitable for caseless comparisions ip_str = ip_str.casefold() # make a dictionary with each vowel a key and value 0 count = {}.fromkeys(vowels,0) # count the vowels for char in ip_str: if char in count: count[char] += 1 print(count) ``` **輸出** ```py {'o': 5, 'i': 3, 'a': 2, 'e': 5, 'u': 3} ``` 在這里,我們采用了存儲在`ip_str`中的字符串。 使用方法`casefold()`,我們使其適合無條件比較。 基本上,此方法返回字符串的小寫版本。 我們使用字典方法`fromkeys()`來構造一個新字典,其中每個元音作為其鍵且所有值等于 0。這是計數的初始化。 接下來,我們使用[循環](/python-programming/for-loop "Python for loop")遍歷輸入字符串。 在每次迭代中,我們檢查字符是否在字典鍵中(如果為元音則為`True`),如果為`true`,則將值增加 1。 * * * ## 源代碼:使用列表和字典推導式 ```py # Using dictionary and list comprehension ip_str = 'Hello, have you tried our tutorial section yet?' # make it suitable for caseless comparisions ip_str = ip_str.casefold() # count the vowels count = {x:sum([1 for char in ip_str if char == x]) for x in 'aeiou'} print(count) ``` 該程序的**輸出**與上述相同。 在這里,我們將[列表推導式](/python-programming/list)嵌套在[字典推導式](/python-programming/dictionary "Dictionary in Python")內,以在單行中對元音進行計數。 但是,由于我們迭代每個元音的整個輸入字符串,因此該程序的速度較慢。
                  <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>

                              哎呀哎呀视频在线观看