<legend id='I5bWb'><style id='I5bWb'><dir id='I5bWb'><q id='I5bWb'></q></dir></style></legend>

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

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

        <bdo id='I5bWb'></bdo><ul id='I5bWb'></ul>
      <tfoot id='I5bWb'></tfoot>

      1. 交错两个字符串的最pythonic方法

        时间:2024-04-20

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

              <legend id='fiS0k'><style id='fiS0k'><dir id='fiS0k'><q id='fiS0k'></q></dir></style></legend>
                <bdo id='fiS0k'></bdo><ul id='fiS0k'></ul>

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

                    <tbody id='fiS0k'></tbody>

                  本文介绍了交错两个字符串的最pythonic方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  将两个字符串连接在一起的最 Pythonic 方式是什么?

                  What's the most pythonic way to mesh two strings together?

                  例如:

                  输入:

                  u = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
                  l = 'abcdefghijklmnopqrstuvwxyz'
                  

                  输出:

                  'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz'
                  

                  推荐答案

                  对我来说,最 Pythonic* 的方法是以下 几乎做同样的事情,但使用 + 用于连接每个字符串中的单个字符的运算符:

                  For me, the most pythonic* way is the following which pretty much does the same thing but uses the + operator for concatenating the individual characters in each string:

                  res = "".join(i + j for i, j in zip(u, l))
                  print(res)
                  # 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz'
                  

                  它也比使用两个 join() 调用更快:

                  It is also faster than using two join() calls:

                  In [5]: l1 = 'A' * 1000000; l2 = 'a' * 1000000
                  
                  In [6]: %timeit "".join("".join(item) for item in zip(l1, l2))
                  1 loops, best of 3: 442 ms per loop
                  
                  In [7]: %timeit "".join(i + j for i, j in zip(l1, l2))
                  1 loops, best of 3: 360 ms per loop
                  

                  存在更快的方法,但它们经常混淆代码.

                  Faster approaches exist, but they often obfuscate the code.

                  注意:如果两个输入字符串不同长度相同,那么较长的字符串将被截断为zip 在较短字符串的末尾停止迭代.在这种情况下,应该使用 而不是 zipzip_longest (izip_longest 在 Python 2) 中来自 itertools 模块以确保两个字符串都完全耗尽.

                  Note: If the two input strings are not the same length then the longer one will be truncated as zip stops iterating at the end of the shorter string. In this case instead of zip one should use zip_longest (izip_longest in Python 2) from the itertools module to ensure that both strings are fully exhausted.

                  *引用 Python 之禅:可读性很重要.
                  Pythonic = 可读性 对我来说;i + j 只是在视觉上更容易解析,至少对我来说是这样.

                  *To take a quote from the Zen of Python: Readability counts.
                  Pythonic = readability for me; i + j is just visually parsed more easily, at least for my eyes.

                  这篇关于交错两个字符串的最pythonic方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Pandas 按递增顺序编号组内的行数 下一篇:如何打印不带括号、逗号和引号的整数列表?

                  相关文章

                  <legend id='iW9HR'><style id='iW9HR'><dir id='iW9HR'><q id='iW9HR'></q></dir></style></legend>

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

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