python基于字符串位置移动字符

python基于字符串位置移动字符,python,string,position,character,move,Python,String,Position,Character,Move,我正在尝试将字符串中的特殊字符从一个位置移动到另一个位置。我希望它出现在下一个角色之后。这是我的字符串: "th i s. i s. a. n i c ^ e . s t r i ng." 此符号可以出现在任何位置。我已经能够确定下一个空间,但我仍然无法移动它 到目前为止,我所做的是: x= "th i s. i s. a. n i c ^ e . s t r i ng." for i in range(len(x)): if x[i] == '^': j = i +

我正在尝试将字符串中的特殊字符从一个位置移动到另一个位置。我希望它出现在下一个角色之后。这是我的字符串:

"th i s. i s. a. n i c ^ e . s t r i ng."
此符号可以出现在任何位置。我已经能够确定下一个空间,但我仍然无法移动它

到目前为止,我所做的是:

x= "th i s. i s. a. n i c ^ e . s t r i ng."
for i in range(len(x)):
    if x[i] == '^':
        j = i + 1
        if x[j] == ' ':
            j = j + 1
            while j < len(x) and x[j] != ' ':
                j = j + 1

            print "in the position " + str(i) + ", I found a hat: '" + str(x[i]) + "'"
            print "in the position " + str(j) + ", I found the next space: '" + str(x[j]) + "'"
            x.remove(i)
            x.insert('^', j)
        else:
            print 'somebody help!'
x=“th i s.i s.a.n i c^e.s t r ING。”
对于范围内的i(len(x)):
如果x[i]='^':
j=i+1
如果x[j]='':
j=j+1
而j
字符串是不可变的,它们没有任何
remove
insert
方法。因此,您应该先将字符串转换为列表,然后才能使用
list.remove
list.insert

>>> x = "th i s. i s. a. n i c ^ e . s t r i ng."
>>> list(x)
['t', 'h', ' ', 'i', ' ', 's', '.', ' ', 'i', ' ', 's', '.', ' ', 'a', '.', ' ', 'n', ' ', 'i', ' ', 'c', ' ', '^', ' ', 'e', ' ', '.', ' ', 's', ' ', 't', ' ', 'r', ' ', 'i', ' ', 'n', 'g', '.']
最后,在修改列表后,您可以使用
str.join
将其重新加入

代码中的错误:

 x.remove('^')     # you should remove '^' here, not the index
 x.insert(j, '^')  # first argument is the index, second is the item
chars = 'th i s. i s. a. n i c  ^e . s t r i ng.'
char = '^'

# Move character to the end of the string.
print move_char_index(chars, char, len(chars)) 
# Result: th i s. i s. a. n i c  e . s t r i ng.^

# Move character to the start of the string.
print move_char_index(chars, char, 0) 
# Result:  ^th i s. i s. a. n i c  e . s t r i ng.
chars = 'th i s. i s. a. n i c  ^e . s t r i ng.'
char = '^'

# Move character forward by 1.
print move_char_by_increment(chars, char, 1) 
# Result: th i s. i s. a. n i c  e^ . s t r i ng.

# Move character backward by 1.
print move_char_by_increment(chars, char, -1) 
# Result: th i s. i s. a. n i c ^ e . s t r i ng.

字符串是不可变的,它们没有任何
remove
insert
方法。因此,您应该先将字符串转换为列表,然后才能使用
list.remove
list.insert

>>> x = "th i s. i s. a. n i c ^ e . s t r i ng."
>>> list(x)
['t', 'h', ' ', 'i', ' ', 's', '.', ' ', 'i', ' ', 's', '.', ' ', 'a', '.', ' ', 'n', ' ', 'i', ' ', 'c', ' ', '^', ' ', 'e', ' ', '.', ' ', 's', ' ', 't', ' ', 'r', ' ', 'i', ' ', 'n', 'g', '.']
最后,在修改列表后,您可以使用
str.join
将其重新加入

代码中的错误:

 x.remove('^')     # you should remove '^' here, not the index
 x.insert(j, '^')  # first argument is the index, second is the item
chars = 'th i s. i s. a. n i c  ^e . s t r i ng.'
char = '^'

# Move character to the end of the string.
print move_char_index(chars, char, len(chars)) 
# Result: th i s. i s. a. n i c  e . s t r i ng.^

# Move character to the start of the string.
print move_char_index(chars, char, 0) 
# Result:  ^th i s. i s. a. n i c  e . s t r i ng.
chars = 'th i s. i s. a. n i c  ^e . s t r i ng.'
char = '^'

# Move character forward by 1.
print move_char_by_increment(chars, char, 1) 
# Result: th i s. i s. a. n i c  e^ . s t r i ng.

# Move character backward by 1.
print move_char_by_increment(chars, char, -1) 
# Result: th i s. i s. a. n i c ^ e . s t r i ng.

我不确定我是否完全理解这个问题,但这里有几个例子,说明如何在字符序列中移动字符

将字符移动到索引 示例:

 x.remove('^')     # you should remove '^' here, not the index
 x.insert(j, '^')  # first argument is the index, second is the item
chars = 'th i s. i s. a. n i c  ^e . s t r i ng.'
char = '^'

# Move character to the end of the string.
print move_char_index(chars, char, len(chars)) 
# Result: th i s. i s. a. n i c  e . s t r i ng.^

# Move character to the start of the string.
print move_char_index(chars, char, 0) 
# Result:  ^th i s. i s. a. n i c  e . s t r i ng.
chars = 'th i s. i s. a. n i c  ^e . s t r i ng.'
char = '^'

# Move character forward by 1.
print move_char_by_increment(chars, char, 1) 
# Result: th i s. i s. a. n i c  e^ . s t r i ng.

# Move character backward by 1.
print move_char_by_increment(chars, char, -1) 
# Result: th i s. i s. a. n i c ^ e . s t r i ng.
增量移动字符 示例:

 x.remove('^')     # you should remove '^' here, not the index
 x.insert(j, '^')  # first argument is the index, second is the item
chars = 'th i s. i s. a. n i c  ^e . s t r i ng.'
char = '^'

# Move character to the end of the string.
print move_char_index(chars, char, len(chars)) 
# Result: th i s. i s. a. n i c  e . s t r i ng.^

# Move character to the start of the string.
print move_char_index(chars, char, 0) 
# Result:  ^th i s. i s. a. n i c  e . s t r i ng.
chars = 'th i s. i s. a. n i c  ^e . s t r i ng.'
char = '^'

# Move character forward by 1.
print move_char_by_increment(chars, char, 1) 
# Result: th i s. i s. a. n i c  e^ . s t r i ng.

# Move character backward by 1.
print move_char_by_increment(chars, char, -1) 
# Result: th i s. i s. a. n i c ^ e . s t r i ng.

我不确定我是否完全理解这个问题,但这里有几个例子,说明如何在字符序列中移动字符

将字符移动到索引 示例:

 x.remove('^')     # you should remove '^' here, not the index
 x.insert(j, '^')  # first argument is the index, second is the item
chars = 'th i s. i s. a. n i c  ^e . s t r i ng.'
char = '^'

# Move character to the end of the string.
print move_char_index(chars, char, len(chars)) 
# Result: th i s. i s. a. n i c  e . s t r i ng.^

# Move character to the start of the string.
print move_char_index(chars, char, 0) 
# Result:  ^th i s. i s. a. n i c  e . s t r i ng.
chars = 'th i s. i s. a. n i c  ^e . s t r i ng.'
char = '^'

# Move character forward by 1.
print move_char_by_increment(chars, char, 1) 
# Result: th i s. i s. a. n i c  e^ . s t r i ng.

# Move character backward by 1.
print move_char_by_increment(chars, char, -1) 
# Result: th i s. i s. a. n i c ^ e . s t r i ng.
增量移动字符 示例:

 x.remove('^')     # you should remove '^' here, not the index
 x.insert(j, '^')  # first argument is the index, second is the item
chars = 'th i s. i s. a. n i c  ^e . s t r i ng.'
char = '^'

# Move character to the end of the string.
print move_char_index(chars, char, len(chars)) 
# Result: th i s. i s. a. n i c  e . s t r i ng.^

# Move character to the start of the string.
print move_char_index(chars, char, 0) 
# Result:  ^th i s. i s. a. n i c  e . s t r i ng.
chars = 'th i s. i s. a. n i c  ^e . s t r i ng.'
char = '^'

# Move character forward by 1.
print move_char_by_increment(chars, char, 1) 
# Result: th i s. i s. a. n i c  e^ . s t r i ng.

# Move character backward by 1.
print move_char_by_increment(chars, char, -1) 
# Result: th i s. i s. a. n i c ^ e . s t r i ng.
[更新]

谢谢你的回复。经过一段时间的纠结,我找到了解决我自己问题的办法。我希望这对其他人有帮助!:-)

x=“th i s.i s.a.n i^c e.s s s t.s t r i ng。”
对于范围内的i(len(x)):
如果x[i]='^':
j=i+1
如果x[j]='':
j=j+1
而j
结果: th i s。i s。A.n i c^e。s s t。s t r i ng.

[更新]

谢谢你的回复。经过一段时间的纠结,我找到了解决我自己问题的办法。我希望这对其他人有帮助!:-)

x=“th i s.i s.a.n i^c e.s s s t.s t r i ng。”
对于范围内的i(len(x)):
如果x[i]='^':
j=i+1
如果x[j]='':
j=j+1
而j
结果:
th i s。i s。A.n i c^e。s s t。s t r i ng.

input
x=“th i s.i s.a.n i c^e.s t r i ng。”
您希望输出是什么?字符串是不可变的。。。您需要创建一个新字符串…输入
x=“th i s.i s.a.n i c^e.s t r i ng。”
您希望输出是什么?字符串是不可变的。。。您将需要创建一个新字符串…如果有一个特殊字符
x。拆分('^')
可能会更快。只有第二个子字符串需要进行分析和更新,然后才能将二者重新连接在一起。我是python新手,注意到列表和字符串的函数之间的差距有点可怕,但可以通过一些技巧加以克服。感谢您的回答,这肯定会对将来的代码有所帮助。:-)如果只有一个特殊字符
x.split('^')
可能会更快。只有第二个子字符串需要进行分析和更新,然后才能将二者重新连接在一起。我是python新手,注意到列表和字符串的函数之间的差距有点可怕,但可以通过一些技巧加以克服。感谢您的回答,这肯定会对将来的代码有所帮助。:-)