1

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

"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!'
4

3 回答 3

3

字符串是不可变的,它们没有任何removeinsert方法。因此,您应该先将字符串转换为列表,然后才能使用list.removeand 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
于 2013-11-07T20:20:14.623 回答
1

我不确定我是否完全理解这个问题,但这里有几个关于如何在字符序列中移动字符的示例。

将字符移动到索引

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.
于 2013-11-07T22:00:08.630 回答
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()

结果:我是。我很好^ e。ss t。细绳。

于 2013-11-08T11:38:43.997 回答