• <small id='OYGmR'></small><noframes id='OYGmR'>

    1. <legend id='OYGmR'><style id='OYGmR'><dir id='OYGmR'><q id='OYGmR'></q></dir></style></legend>
      • <bdo id='OYGmR'></bdo><ul id='OYGmR'></ul>

        <tfoot id='OYGmR'></tfoot>
        <i id='OYGmR'><tr id='OYGmR'><dt id='OYGmR'><q id='OYGmR'><span id='OYGmR'><b id='OYGmR'><form id='OYGmR'><ins id='OYGmR'></ins><ul id='OYGmR'></ul><sub id='OYGmR'></sub></form><legend id='OYGmR'></legend><bdo id='OYGmR'><pre id='OYGmR'><center id='OYGmR'></center></pre></bdo></b><th id='OYGmR'></th></span></q></dt></tr></i><div id='OYGmR'><tfoot id='OYGmR'></tfoot><dl id='OYGmR'><fieldset id='OYGmR'></fieldset></dl></div>
      1. Python,字符串中的特定字符数

        时间:2023-08-30
            1. <i id='js1Nv'><tr id='js1Nv'><dt id='js1Nv'><q id='js1Nv'><span id='js1Nv'><b id='js1Nv'><form id='js1Nv'><ins id='js1Nv'></ins><ul id='js1Nv'></ul><sub id='js1Nv'></sub></form><legend id='js1Nv'></legend><bdo id='js1Nv'><pre id='js1Nv'><center id='js1Nv'></center></pre></bdo></b><th id='js1Nv'></th></span></q></dt></tr></i><div id='js1Nv'><tfoot id='js1Nv'></tfoot><dl id='js1Nv'><fieldset id='js1Nv'></fieldset></dl></div>
              <legend id='js1Nv'><style id='js1Nv'><dir id='js1Nv'><q id='js1Nv'></q></dir></style></legend>
                <tbody id='js1Nv'></tbody>
            2. <tfoot id='js1Nv'></tfoot>

                <small id='js1Nv'></small><noframes id='js1Nv'>

                  <bdo id='js1Nv'></bdo><ul id='js1Nv'></ul>
                • 本文介绍了Python,字符串中的特定字符数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试计算 python 中字符串中出现的次数.我想采用二进制输入,例如001101".然后数出 1、0、11、00 等的个数.

                  I am trying to count the number of occurrences in a string in python. I would like to take a binary input, say '001101'. Then count the number of 1s, 0s, 11s, 00s etc.

                  我试图通过使用计数来实现这一点,但这将输出有 3 个 1,当我只希望它输出 1 个 1 和 1 个 11 并且它不单独计算它们时,除非它们在它们的拥有.

                  I have tried to implement this by using count, but this will output that there are 3 1s, when i only want it to output 1 1, and 1 11s and for it to not count them individually, unless they are on their own.

                  我也试过用 find 来实现这个,但我遇到了同样的问题.

                  I have also tried to implement this with find, but i am having the same problem.

                  任何帮助将不胜感激,谢谢.

                  Any help would be appreciated, thanks.

                  推荐答案

                  您可以使用 itertools.groupbycollections.Counter:

                  You can do the following, using itertools.groupby and collections.Counter:

                  from itertools import groupby
                  from collections import Counter
                  
                  s = '001101011'
                  c = Counter(''.join(g) for _, g in groupby(s))
                  
                  c.get('11')
                  # 2
                  c.get('1')
                  # 1
                  c.get('111', 0)  # use default value to capture count 0 properly
                  # 0
                  

                  这会将字符串分组为仅由相等字符组成的子字符串,并对这些子字符串执行计数.

                  This groups the string into substrings consisting only of equal chars and performs the counting on those substrings.

                  这篇关于Python,字符串中的特定字符数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:BeautifulSoup 在 instagram html 页面中查找 下一篇:使用 Python 查找和删除目录中的特定文件和子目录

                  相关文章

                • <small id='oSKFR'></small><noframes id='oSKFR'>

                  1. <i id='oSKFR'><tr id='oSKFR'><dt id='oSKFR'><q id='oSKFR'><span id='oSKFR'><b id='oSKFR'><form id='oSKFR'><ins id='oSKFR'></ins><ul id='oSKFR'></ul><sub id='oSKFR'></sub></form><legend id='oSKFR'></legend><bdo id='oSKFR'><pre id='oSKFR'><center id='oSKFR'></center></pre></bdo></b><th id='oSKFR'></th></span></q></dt></tr></i><div id='oSKFR'><tfoot id='oSKFR'></tfoot><dl id='oSKFR'><fieldset id='oSKFR'></fieldset></dl></div>

                  2. <legend id='oSKFR'><style id='oSKFR'><dir id='oSKFR'><q id='oSKFR'></q></dir></style></legend>
                      <bdo id='oSKFR'></bdo><ul id='oSKFR'></ul>

                      <tfoot id='oSKFR'></tfoot>