Python 如何修复';类型错误:不可损坏的类型:';列表';错误?

Python 如何修复';类型错误:不可损坏的类型:';列表';错误?,python,python-3.x,Python,Python 3.x,为了让我的程序启动,我需要一个字符串并将数字插入到一个特定的位置。我已经有了这样的代码。我现在需要做的是,获取它,这样程序就会对列表中的每个项目(这是一个数字)执行该操作。我使用了一个内联for循环,得到了一个“TypeError:unhabable type:“list”错误。我还想把结果列在一个列表中 我已经做的是编写代码来替换我正在使用的原始字符串中的数字,作为概念证明。然而,当我试图在前面的代码中插入一个循环时,我得到了那个错误。代码如下: s = '<button id="qty

为了让我的程序启动,我需要一个字符串并将数字插入到一个特定的位置。我已经有了这样的代码。我现在需要做的是,获取它,这样程序就会对列表中的每个项目(这是一个数字)执行该操作。我使用了一个内联for循环,得到了一个“TypeError:unhabable type:“list”错误。我还想把结果列在一个列表中

我已经做的是编写代码来替换我正在使用的原始字符串中的数字,作为概念证明。然而,当我试图在前面的代码中插入一个循环时,我得到了那个错误。代码如下:

s = '<button id="qty_plus_cartline_change_1221067058" type="button" class="quantity_plus " data-component="quantitybox.inc">+<span class="offscreen">Increase the Quantity 1</span></button>'
这是一个概念证明,可以用我想要的方式替换数字

updated_list = []
new_string = re.sub('\d+', [i for i in list], s,1)
updated_list.append(new_string)
print(updated_list)
上面是我试图使用的代码,以便用现有列表中的所有数字替换字符串

因此,我要查找的是一个新字符串,其中包含列表(list)中每个项目的新更新编号。供参考:

list = [1111111111,2222222222,3333333333,4444444444]
这意味着,我要找的是:

['<button id="qty_plus_cartline_change_1111111111" type="button" class="quantity_plus " data-component="quantitybox.inc">+<span class="offscreen">Increase the Quantity 1</span></button>', '<button id="qty_plus_cartline_change_2222222222" type="button" class="quantity_plus " data-component="quantitybox.inc">+<span class="offscreen">Increase the Quantity 1</span></button>', '<button id="qty_plus_cartline_change_3333333333" type="button" class="quantity_plus " data-component="quantitybox.inc">+<span class="offscreen">Increase the Quantity 1</span></button>', '<button id="qty_plus_cartline_change_4444444444" type="button" class="quantity_plus " data-component="quantitybox.inc">+<span class="offscreen">Increase the Quantity 1</span></button>']
['+增加数量1','+增加数量1','+增加数量1','+增加数量1']

非常感谢您的帮助

您应该在列表中调用
re.sub
,而不是相反。另外,不要命名变量
list
,因为它会影响内置函数
list
。在以下示例中,我将其重命名为
lst

updated_list = [re.sub('\d+', str(i), s, 1) for i in lst]

谢谢你,伙计!我会在早上试一试。是的,列表只是这个例子的名字,但你完全正确
updated_list = [re.sub('\d+', str(i), s, 1) for i in lst]