What is the simplest way to swap each pair of adjoining chars in a string with Python?

前端 未结 19 936
名媛妹妹
名媛妹妹 2020-11-30 06:25

I want to swap each pair of characters in a string. \'2143\' becomes \'1234\', \'badcfe\' becomes \'abcdef\'.

How

19条回答
  •  春和景丽
    2020-11-30 07:16

    A bit late to the party, but there is actually a pretty simple way to do this:

    The index sequence you are looking for can be expressed as the sum of two sequences:

     0  1  2  3 ...
    +1 -1 +1 -1 ...
    

    Both are easy to express. The first one is just range(N). A sequence that toggles for each i in that range is i % 2. You can adjust the toggle by scaling and offsetting it:

             i % 2      ->  0  1  0  1 ...
         1 - i % 2      ->  1  0  1  0 ...
    2 * (1 - i % 2)     ->  2  0  2  0 ...
    2 * (1 - i % 2) - 1 -> +1 -1 +1 -1 ...
    

    The entire expression simplifies to i + 1 - 2 * (i % 2), which you can use to join the string almost directly:

    result = ''.join(string[i + 1 - 2 * (i % 2)] for i in range(len(string)))
    

    This will work only for an even-length string, so you can check for overruns using min:

    N = len(string)
    result = ''.join(string[min(i + 1 - 2 * (i % 2), N - 1)] for i in range(N))
    

    Basically a one-liner, doesn't require any iterators beyond a range over the indices, and some very simple integer math.

提交回复
热议问题