如何用小数号列表替换字符串中的小数号列表?

发布于 2025-01-22 04:13:18 字数 374 浏览 1 评论 0原文

我需要用另一个小数号列表替换字符串中的小数号列表。以下是第一次尝试,该尝试更改所有小数号的十进制数字:

>>> re.sub (r"[-+]?\d*\.\d+f?", "1.0", "hello 1.2 3.4")
'hello 1.0 1.0'

我需要诸如my_replace下面的内容:

>>> my_replace (r"[-+]?\d*\.\d+f?", [1.0, 2.0], "hello 1.2 3.4")
'hello 1.0 2.0'

如何使用Python的RE模块实现my_replace

I need to replace the list of decimal numbers in a string with another list of decimal numbers. The following is a first try, that changes all decimal numbers with the same decimal number:

>>> re.sub (r"[-+]?\d*\.\d+f?", "1.0", "hello 1.2 3.4")
'hello 1.0 1.0'

I need something like my_replace below:

>>> my_replace (r"[-+]?\d*\.\d+f?", [1.0, 2.0], "hello 1.2 3.4")
'hello 1.0 2.0'

How can i implement my_replace with python's re module?

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

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

发布评论

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

评论(1

自此以后,行同陌路 2025-01-29 04:13:18

我认为您不能将列表用作替换变量并在它们上迭代。因此,它无法处理不可用的对象(这是Python抱怨的)。但是它也能够处理数字(所以它需要一个字符串列表,但这显然是假设的XD),

我只会循环绕字符串,并兼容所有不是十进制数字的所有内容找到十进制数字。

text = "hello 1.2 3.4 don't replace an integer: 9 but this decimal number is too much: 0.0 (so use last value!)"
new_numbers = [42, 3.1415926535]

new_text = ''
idx_last = 0
for i, tx in enumerate(re.finditer(r'[-+]?\d*\.\d+f?', text)):
    # add text before the number
    new_text += tx.string[idx_last:tx.start()]
    # add new number (but ensure that your are not overflowing the list of new numbers
    new_text += str(new_numbers[min([i, len(new_numbers) - 1])])
    # update text index
    idx_last = tx.end()
# update remaining part of the text
new_text += text[idx_last:]

“ Hello 42 3.1415926535不要替换整数:9,但是该小数的数字太多了:3.1415926535(请使用最后一个值!)”

将其包装到函数,并且您的my_replace() =)

I don't think that you can use a list as replacement variables and iterate over them. So it can't handle unhashable objects (this is what python is complaining about). But it wouln'd be able to handle numerics as well (so it would need a list of strings but this is obviously hypothetical xD)

I would just loop over the string and compy everything that is not a decimal number to a new string and replacing the decimal numbers found.

text = "hello 1.2 3.4 don't replace an integer: 9 but this decimal number is too much: 0.0 (so use last value!)"
new_numbers = [42, 3.1415926535]

new_text = ''
idx_last = 0
for i, tx in enumerate(re.finditer(r'[-+]?\d*\.\d+f?', text)):
    # add text before the number
    new_text += tx.string[idx_last:tx.start()]
    # add new number (but ensure that your are not overflowing the list of new numbers
    new_text += str(new_numbers[min([i, len(new_numbers) - 1])])
    # update text index
    idx_last = tx.end()
# update remaining part of the text
new_text += text[idx_last:]

"hello 42 3.1415926535 don't replace an integer: 9 but this decimal number is too much: 3.1415926535 (so use last value!)"

Wrap it to a function and you have your my_replace() =)

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