[英]Converting string using list in python
I've been trying to rearrange the string by reversing a particular strings consecutively from the given string input and the limit is given as input. 我一直在尝试通过从给定的字符串输入中连续反转特定的字符串来重新排列字符串,并且将限制作为输入。
for example limit is 3 例如限制是3
if input is Hellothegamestarts 如果输入是Hellothegamestarts
output must be Heltolhegemastastr and it is saved in separate array The code is: 输出必须是Heltolhegemastastr,并且必须保存在单独的数组中。代码为:
while True:
t = int(input())
if t == 0:
break
string = raw_input()
string = string.encode('utf-8')
leng = len(string)
r = t/leng
m = []
leng = 0
for i in range(r):
if r % 2 == 0:
l = 0
l = leng + t
for i in range(t):
temp = string[l]
m.append(temp)
l = l - 1
r = r + 1
leng = leng + t
else:
l = 0
l = leng
for i in range(t):
temp = string[l]
m.append(temp)
l = l + 1
r = r + 1
leng = leng + t
print m
the output i got is [] and asks for next input for t. 我得到的输出是[],并要求下一个输入t。
Any help is appreciated. 任何帮助表示赞赏。
Take the blocks in chunks of 3s, and reverse the odd ones, eg: 以3s的块为单位,并反转奇数,例如:
import re
s = 'Hellothegamestarts'
r = ''.join(
el if idx % 2 == 0 else el[::-1]
for idx, el in enumerate(re.findall('.{,3}', s))
)
# Heltolhegemastastr
Maybe you can try - 也许您可以尝试-
t = int(input())
if t == 0:
break;
string = raw_input()
m = ''
leng = len(string)
i = 0
while i < leng:
if (i/t) % 2 != 0:
m = m + string[i+t-1:i-1:-1]
else:
m = m + string[i:i+t]
i = i + t
print(m)
Alternatively you can try this 另外,您可以尝试一下
def myfunc(s, count):
return [''.join(x) for x in zip(*[list(s[z::count]) for z in range(count)])]
a='Hellothegamestarts'
lst=myfunc(a,3)
print "".join([i if lst.index(i) in range(0,len(lst),2) else i[::-1] for i in lst])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.