Python 属性错误:';列表';对象没有属性';更换';out=[j.replace(“on”和“re”)表示j in out]

Python 属性错误:';列表';对象没有属性';更换';out=[j.replace(“on”和“re”)表示j in out],python,list,for-loop,replace,attributeerror,Python,List,For Loop,Replace,Attributeerror,我正在尝试用用户词替换这些词。这两个词都取自用户。但我不知道这里面出了什么问题 def practiseeight(): number = str(request.args.get('num')) on = str(request.args.get('one')) re = str(request.args.get('two')) value = number.split('<') print(value) out = [] for

我正在尝试用用户词替换这些词。这两个词都取自用户。但我不知道这里面出了什么问题

def practiseeight():
    number = str(request.args.get('num'))
    on = str(request.args.get('one'))
    re = str(request.args.get('two'))
    value = number.split('<')
    print(value)
    out = []
    for i in value:
        i = i.split('>')
        out.append(i)
        print("This is",out)
        
     
    out = [j.replace("on", "re") for j in out]
    print("new list", out)
def practiseeight():
number=str(request.args.get('num'))
on=str(request.args.get('one'))
re=str(request.args.get('two'))
值=数字。拆分(“”)
out.append(i)
打印(“这是”,输出)
out=[j.替换(“on”,“re”)表示j in out]
打印(“新列表”,输出)
代码中的问题:

out = []
    for i in value:
        i = i.split('>') # so you are splitting i, split return a list
        out.append(i) # you are appending i which is a list
        print("This is",out)
    out = [j.replace("on", "re") for j in out] # now you are going through each element in out, each element is a list. List do not have replace, strings do!
要在列表中替换,请执行以下操作:

out = [['abc'], ['abc', 'bcd']]
for i in out: # go through each element in out
    for j,v in enumerate(i): i[j] = v.replace('b','e') # go through each element in the list of list and replace
print(out)

正如其他答案正确地提到的,您的变量“out”成为列表的列表,因为您将“i”(这是一个列表)附加到变量“out”中。尝试改用“out.extend”

out = []
for i in value:
    i = i.split('>')
    out.extend(i)  ## this will add the element 'i' to the end of the existing list 'out'
    print("This is",out)
    
 
out = [j.replace("on", "re") for j in out]
print("new list", out)

尝试在
输出中打印值以查看它们是什么
i=i.split('>')
创建了一个列表,这就是列表中的内容。因为我们没有你的数据,我们也不知道替换应该做什么,所以我们不能做更多的事情。这是我对以上内容的输入code@kuldeepSingSindhu如果您有时间,请您解释一下second for loop(对于枚举(i)中的j,v:i[j]=v.replace('b','e'))的详细理解,我们将不胜感激。
enumerate
遍历该列表并为您提供索引,该索引处的值。我们获取该值,对其进行替换,然后在该索引处更新列表。上面的j是指数,v是数值。你可以把它们打印出来,以获得更好的想法!
out = []
for i in value:
    i = i.split('>')
    out.extend(i)  ## this will add the element 'i' to the end of the existing list 'out'
    print("This is",out)
    
 
out = [j.replace("on", "re") for j in out]
print("new list", out)