Python 如何通过替换';来扩充包含(0,1,2,3,4,5,6,?)的长字符串';和最大的相邻数?

Python 如何通过替换';来扩充包含(0,1,2,3,4,5,6,?)的长字符串';和最大的相邻数?,python,string,loops,data-augmentation,Python,String,Loops,Data Augmentation,我试图扩充一个可以包含多个数字的长字符串(0,1,2,3,4,5,6,?) 考虑一个字符串“00000000000 4?0??100001??2?0?10000000”< /代码>。 我试图用相邻的最大数字替换所有问号(?)。比较应该从左字符和右字符到问号(?) 输入字符串:“000000000004?0?100001?2?0?10000000” 输出字符串:“00000000000440111000012220110000000” 我编写了一个函数,最终在循环本身的第一次迭代中替换它们,结果是

我试图扩充一个可以包含多个数字的长字符串
(0,1,2,3,4,5,6,?)
考虑一个字符串<代码>“00000000000 4?0??100001??2?0?10000000”< /代码>。 我试图用相邻的最大数字替换所有问号
(?)
。比较应该从左字符和右字符到问号
(?)

输入字符串:“000000000004?0?100001?2?0?10000000” 输出字符串:
“00000000000440111000012220110000000”

我编写了一个函数,最终在循环本身的第一次迭代中替换它们,结果是将
替换为最大的数字,即在本例中为4。检查下面的代码片段

输出错误:
“000000000004404410000144240410000000”

def方法扩充(八月街):
秒字符、秒字符、前字符的全局位置
标志\标记\字符串\结束=0
列表_predef=['0','1','2','3','4','5','6']
len_aug_str=len(aug_str)
对于范围(0,(len_aug_str-1))内的m:
如果aug_str[m]=='?':
第一个问题的位置=m
如果m!=0:
进站字符=八月街[m-1]
#打印(“前字符:”,前字符)
对于范围内的n((第一个问题的位置+1),(第二个问题的位置-1)):
如果列表中的aug\u str[n]:
第二字符的位置=n
sec_char=aug_str[n]
打印(秒字符)
如果前字符>秒字符:
aug_str=aug_str.replace(aug_str[第一个问题的位置],前字符)
前字符、秒字符、第一个字符的位置,m
其他:
aug_str=aug_str.替换(aug_str[第一个问题的位置],第二个字符)
前字符、秒字符、首字符位置
打破
其他:
标志\标记\字符串\结束+=1
其他:
对于范围内的q((第一个问题的位置+1),(第二个问题的位置-1)):
如果列表中的aug_str[q]:
第二字符的位置=q
sec_char=aug_str[q]
aug_str=aug_str.替换(aug_str[第一个问题的位置],第二个字符)
打破
#如果前字符>秒字符:
#八月街=八月街替换(八月街[m],前字符)
#其他:
#八月街=八月街替换(八月街[m],秒字符)
其他:
持续
返回奥古街
输入字符串:“000000000004?0?100001?2?0?10000000”

预期输出字符串:
“00000000000440111000012220110000000”

实际输出字符串:
“000000000004404410000144240410000000”


有多个这样的字符串,具有不同的数字和
组合。我希望我已经解释清楚了。请帮忙。谢谢。

你的程序听起来太复杂了。我甚至没有试着去理解。你能读懂这个吗

import re


def method_augment(text: str) -> str:
    while "?" in text:
        text = replacesingle("0" + text + "0")[1:-1]   # consider starting and ending question marks
    return text


def replacesingle(text: str) -> str:
    match = re.search("\\d\\?+\\d", text)
    span = match.span(0)
    partialtext = text[span[0]:span[1]]
    left = int(partialtext[0])
    right = int(partialtext[-1])
    larger = left if left > right else right
    number_of_question_marks = len(partialtext) - 2
    text = text[:span[0] + 1] + str(larger) * number_of_question_marks + text[span[1] - 1:]
    return text


assert(method_augment("000000000004?0??100001??2?0?10000000") == "000000000004401110000122220110000000")
assert (method_augment("??123??1??") == "1112333111")

下面是一种方法,通过一些测试:

def replace_with_largest(s):
    out = []
    last_left = None
    next_right = None
    for i, c in enumerate(s):
        if c in '0123456789':
            out.append(c)
            last_left = c
            next_right = None # now the next digit to the right is unknown
            continue

        # Now we have a '?'.
        # We need the next digit to the right
        if next_right is None:
            for j in range(i+1, len(s)):
                if s[j] != '?':
                    next_right = s[j]
                    break
            else:
                # No more digit right of here, we'll use the last one on the left
                next_right = last_left

        out.append(max(last_left, next_right) if last_left is not None else next_right)    

    return ''.join(out)
测试、一些字符串和预期输出:

tests = [("000000000004?0??100001??2?0?10000000", "000000000004401110000122220110000000"),
         ("??123??1", "11123331"),
         ("123???", "123333")]


for test in tests:
    print(test[0], replace_with_largest(test[0]), replace_with_largest(test[0]) == test[1])

000000000004?0??100001??2?0?10000000 000000000004401110000122220110000000 True
??123??1 11123331 True
123??? 123333 True

我不确定这是否有效,但您可以将列表拆分为保留
s的所有连续值,并将其作为单独的元素放在一起,用前导字符和尾随字符填充该列表,与数字或
相比,该列表永远不会通过
最大值测试(同时也使访问索引更加方便),例如:

然后使用它,例如:

cases = [
    '000000000004?0??100001??2?0?10000000',
    '?????????9',
    '9????????0',
    '0????????9',
    '?0???????9',
    '123???????',
    '12?????321',
    '??????????',
]

for case in cases:
    print(case, '->', augment(case))
这给了你:

000000000004?0??100001??2?0?10000000 -> 000000000004401110000122220110000000
?????????9 -> 9999999999
9????????0 -> 9999999990
0????????9 -> 0999999999
?0???????9 -> 0099999999
123??????? -> 1233333333
12?????321 -> 1233333321
?????????? -> None

这在
method\u augment(“?123?1”)
AttributeError上失败:“NoneType”对象没有属性“span”
要求不是很清楚,所以我假设“任意数量的????任意位置”…感谢很多Jon。我用多个字符串变体尝试了它。工作起来像专业版。
000000000004?0??100001??2?0?10000000 -> 000000000004401110000122220110000000
?????????9 -> 9999999999
9????????0 -> 9999999990
0????????9 -> 0999999999
?0???????9 -> 0099999999
123??????? -> 1233333333
12?????321 -> 1233333321
?????????? -> None