用例 1:
str = '?0?' #str is input string , we need to replace every '?' with [0,1].
所以結果輸出將是:
['000','100','001','101']
用例 2:
str = '?0' #input
預期輸出:
['00','10']
用例 3:
str='?'
預期輸出:
['0','1']
字串的長度和 '?' 的個數 in string 可能因不同的輸入而異。
uj5u.com熱心網友回復:
這是一個解決方案,用于為 a或數字本身itertools.product生成所有可能的產品:[0,1]?
str = '?0?'
[''.join(p) for p in itertools.product(*[['0', '1'] if c == '?' else c for c in str])]
輸出:
['000', '001', '100', '101']
uj5u.com熱心網友回復:
沒有迭代工具:
s = '?0?'
a = ['']
for c in s:
a = [b d for b in a for d in c.replace('?', '01')]
print(a)
在線嘗試!
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/490476.html
