Я вот не дописал,
def test(text): res = '' count = 0; for t in text: res += text[1 + count] + text[0 + count] count += 2 return res
Как можно сделать чтобы с каждой итерацией
к text прибавлялось +2 ?
Не пойму почему count не хочет прибавляться ?
def test(text): res = '' count = 0; for t in text: res += text[1 + count] + text[0 + count] count += 2 return res
def test(text): res = '' count = 0; for t in text: try: res += text[1 + count] + text[0 + count] count += 2 except IndexError: pass return res
def test1(text): return ''.join([x + y for x, y in zip(text[1::2], text[::2])]) def test2(text): res = [] for i in range(0, len(text) - 1, 2): res.append(text[i + 1] + text[i]) return ''.join(res) def test3(text): return ''.join([text[i + 1] + text[i] for i in range(0, len(text) - 1, 2)])
И цикл будет крутиться в два раза больше чем нужно. Хотя бы break вместо pass нужен.except IndexError: pass
def shuffle_str(text): res = '' for t in range(0,len(text),2): res += text[1 + t] + text[0 + t] return res
from array import array def swap(txt): arr=array('H', txt) arr.byteswap() return a.tostring()
odnochlenДа, кстати. Но с Юникодом не справиться (если вообще это кому-то нужно будет
Самое быстрое.
from array import array
).