如何在字符串中移动空白?

发布于 2025-01-29 20:04:12 字数 456 浏览 2 评论 0原文

我需要将一个弦的空格移动到右侧的一个位置。 这是我的代码:

for i in range(0,len(resultaat)):
    if resultaat[i] == " ":
        string = resultaat[:i] + resultaat[i+1] + " " + resultaat[i+2:]

例如: 如果结果=

"TH EZE NO  FPYTHON."

我的输出需要:

'THE ZEN OF PYTHON.'

,但是我得到的输出是:

"TH EZE NO F PYTHON."

我认为这发生了,因为循环解开了它移动上一个空间的动作。 我不知道如何解决这个问题。 有人可以帮我吗?

谢谢!

I need to move a whitespace in a string one position to the right.
This is my code:

for i in range(0,len(resultaat)):
    if resultaat[i] == " ":
        string = resultaat[:i] + resultaat[i+1] + " " + resultaat[i+2:]

E.g.:
If resultaat =

"TH EZE NO  FPYTHON."

Than my output needs to be:

'THE ZEN OF PYTHON.'

, but the output that I get is:

"TH EZE NO F PYTHON."

I think this happened because the loop undid the action where it moved the previous space.
I don't know how to fix this problem.
Can someone help me with this?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吾性傲以野 2025-02-05 20:04:12

每次通过循环,您都将获得原始Resultaat字符串的切片,而无需对以前的迭代进行更改。

首先,您应该将RESTERAAT将其复制到String首先,然后将其用作每个切片的来源,以便您累积所有更改。

string = resultaat
for i in range(0,len(resultaat)):
    if resultaat[i] == " ":
        string = string[:i] + string[i+1] + " " + string[i+2:]

Each time through the loop you're getting slices of the original resultaat string, without the changes you've made for previous iterations.

You should copy resultaat to string first, then use that as the source of each slice so you accumulate all the changes.

string = resultaat
for i in range(0,len(resultaat)):
    if resultaat[i] == " ":
        string = string[:i] + string[i+1] + " " + string[i+2:]
滥情空心 2025-02-05 20:04:12

您可以做这样的事情:

# first get the indexes that the character you want to merge
indexes = [i for i, c in enumerate(resultaat) if c == ' ']
for i in indexes: # go through those indexes and swap the characters as you have done
    resultaat = resultaat[:i] + resultaat[i+1] + " " + resultaat[i+2:] # updating resultaat each time you want to swap characters

You could do something like this:

# first get the indexes that the character you want to merge
indexes = [i for i, c in enumerate(resultaat) if c == ' ']
for i in indexes: # go through those indexes and swap the characters as you have done
    resultaat = resultaat[:i] + resultaat[i+1] + " " + resultaat[i+2:] # updating resultaat each time you want to swap characters
决绝 2025-02-05 20:04:12

假设所述的输入值实际上比实际需要的空间多:

TXT = "TH EZE NO FPYTHON."

def process(s):
    t = list(s)
    for i, c in enumerate(t[:-1]):
        if c == ' ':
            t[i+1], t[i] = ' ', t[i+1]
    return ''.join(t)

print(process(TXT))

输出:

THE ZEN OF PYTHON.

Assuming the stated input value actually has one more space than is actually needed then:

TXT = "TH EZE NO FPYTHON."

def process(s):
    t = list(s)
    for i, c in enumerate(t[:-1]):
        if c == ' ':
            t[i+1], t[i] = ' ', t[i+1]
    return ''.join(t)

print(process(TXT))

Output:

THE ZEN OF PYTHON.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文