简体   繁体   English

如何修复“TypeError: unhashable type: 'list' 错误?

[英]How to fix 'TypeError: unhashable type: 'list' error?

In order to get my program off the ground I need to take a string and insert numbers into a particular position.为了让我的程序开始运行,我需要取一个字符串并将数字插入到特定位置。 I have code that does that already.我已经有代码可以做到这一点。 What I need to do now, is get it so the program will do that for every item (which is a number) in a list.我现在需要做的是获取它,以便程序将对列表中的每个项目(这是一个数字)执行此操作。 I used an inline for loop and have gotten a 'TypeError: unhashable type: 'list' error.我使用了一个内联 for 循环并得到了一个“TypeError: unhashable type: 'list' 错误。 I also want to put the results in a list.我也想把结果放在一个列表中。

What I've already done is write the code that will replace the numbers in the original string that I am working with as a proof of concept.我已经做的是编写代码来替换我正在使用的原始字符串中的数字作为概念证明。 However, when I try to insert a loop into the previous code, I get that error.但是,当我尝试在前面的代码中插入一个循环时,我得到了那个错误。 The code is as follows:代码如下:

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>'
new_string = re.sub('\d+', 'new number', s,1)
print(new_string)

That is the proof of concept that works to replace the numbers the way I want.这是概念证明,可以按照我想要的方式替换数字。

updated_list = []
new_string = re.sub('\d+', [i for i in list], s,1)
updated_list.append(new_string)
print(updated_list)

Above is the code I'm trying to employ in order to replace the string with all of the numbers in the existing list.以上是我试图使用的代码,以便用现有列表中的所有数字替换字符串。

What I'm looking for as a result is a new string with the new updated number for each item in the list (list).结果,我正在寻找的是一个新字符串,其中包含列表(列表)中每个项目的新更新编号。 For reference:以供参考:

list = [1111111111,2222222222,3333333333,4444444444]

This means, what I'm looking for is this:这意味着,我正在寻找的是:

['<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>']

Any help is greatly appreciated!任何帮助是极大的赞赏!

You should call re.sub inside a list comprehension, not the other way around.您应该在列表re.sub调用re.sub ,而不是相反。 Also, don't name a variable list since it would shadow the built-in function list .另外,不要命名变量list因为它会影响内置函数list I've renamed it to lst in the following example:在以下示例中,我已将其重命名为lst

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM