如何将数字返回到字符串中作为反转

时间:2017-10-01 14:38:56

标签: php arrays string loops

我正在练习这个练习,我得到了一个关于程序如何工作的例子

Entry: "Dlasdf234dkl sdfkl8886adaf15adfjk7 asdflkj376661a s445s198"

Output: "Dlasdf432dkl sdfkl6888adaf51adfjk7 asdflkj166673a s544s891"

我设法获得所有数字,无论它们在哪里,我通过爆炸和内爆来做到这一点。我有数字作为字符串,因为我在一个循环中完成它,我被卡住了,不能用原始字符串取回数字。

当我尝试将它们放回字符串中时,我得到了这个混乱:

Array
(
[0] => Array
    (
        [a] => Dlasdf432dkl sdfkl432adaf432adfjk432 asdflkj432a s432s432
        [sub] => Пошаљи
    )

[1] => Array
    (
        [a] => Dlasdf6888dkl sdfkl6888adaf6888adfjk6888 asdflkj6888a s6888s6888
        [sub] => Пошаљи
    )

[2] => Array
    (
        [a] => Dlasdf51dkl sdfkl51adaf51adfjk51 asdflkj51a s51s51
        [sub] => Пошаљи
    )

[3] => Array
    (
        [a] => Dlasdf7dkl sdfkl7adaf7adfjk7 asdflkj7a s7s7
        [sub] => Пошаљи
    )

[4] => Array
    (
        [a] => Dlasdf166673dkl sdfkl166673adaf166673adfjk166673 asdflkj166673a s166673s166673
        [sub] => Пошаљи
    )

[5] => Array
    (
        [a] => Dlasdf544dkl sdfkl544adaf544adfjk544 asdflkj544a s544s544
        [sub] => Пошаљи
    )

[6] => Array
    (
        [a] => Dlasdf891dkl sdfkl891adaf891adfjk891 asdflkj891a s891s891
        [sub] => Пошаљи
    )
)

1 个答案:

答案 0 :(得分:0)

您希望使用正则表达式解析数字并替换它们:

import torch
from torch.autograd import Variable
import torch.nn as nn
inp = Variable(torch.Tensor([1]), requires_grad=True)
target = Variable(torch.Tensor([3]))
loss_fn = nn.MSELoss()
for i in range(2):
    loss = loss_fn(inp, target)
    loss.backward()
    gradient = inp.grad
    inp.data = inp.data - inp.grad.data * 0.01
    inp.grad.data.zero_()