我试图将一个特殊字符从一个位置移动到另一个位置。我希望它出现在下一个角色之后。这是我的字符串:
"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!'
答案 0 :(得分:3)
字符串是不可变的,他们没有任何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
答案 1 :(得分:1)
[更新]
感谢所有出色的回复。在弄乱了一段时间之后,我找到了解决自己问题的方法。我希望这有助于其他人! : - )
x= "th i s. i s. a. n i^ c e. s s t. 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 x
x= x[0:i] + x[i+1:]
x= x[0:j-1] + "^" + x[j-1:]
print x
exit()
结果: 这个。我。一个。是的。 s s。 s t r n ng。
答案 2 :(得分:0)
我不确定我是否完全理解这个问题,但这里有一些关于如何在一系列字符中移动角色的例子。
def move_char_index(chars, char, new_index):
# Convert character sequence to list type.
char_list = list(chars)
# Get the current index of the target character.
old_index = char_list.index(char)
# Remove the target character from the character list.
char = char_list.pop(old_index)
# Insert target character at a new location.
char_list.insert(new_index, char)
# Convert character list back to str type and return.
return ''.join(char_list)
<强>示例:强>
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.
def move_char_by_increment(chars, char, increment):
# Convert character sequence to list type.
char_list = list(chars)
# Get the current index of the target character.
old_index = char_list.index(char)
# Remove the target character from the character list.
char = char_list.pop(old_index)
# Insert target character at a new location.
new_index = old_index + increment
char_list.insert(new_index, char)
# Convert character list back to str type and return.
return ''.join(char_list)
<强>示例:强>
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.