AttributeError: 'list' object has no attribute 'split' - Python

AttributeError: 'list' object has no attribute 'split' - Python

def titleapi(value, list):
    list = str(list)
    list_ = list.split(',')
    print(list_)
    list_2 = list_.split(':')
    print(list_2)
    code = '<ul>'
    var = 0
    for ele in list_2:
        if var == 0:
            var = 1
            if ele == value:
                var_2 = 0
                for ele_2 in list_2:
                    if var_2 == 0:
                        var_2 = 1
                        if ele_2 == ele:
                            code += '\n\t<li><a class="active" href="{}">{}</a></li>'.format(list_2[(list_2.index(str(ele))) + 1], str(ele))
                        else:
                            code += '\n\t<li><a href="{}">{}</a></li>'.format(list_2[(list_2.index(str(ele))) + 1], str(ele))
        else:
            continue

    code += '\n<ul>'
    return str(code)

AttributeError: 'list' 对象没有属性 'split'

我正在尝试将其转换为 return HTML 代码,输入是这样的

titleapi(title, 'Home:#,About:#,Contact:#')

“#”符号只是因为它现在已经死了 link

拆分功能用于字符串,不适用于列表。它 returns 通过作为参数传递的指定分隔符分隔给定字符串后的字符串列表。

您可以尝试将第 3 行中的代码更改为 list_ = str(list.split(','))

,将列表 list_ 转换为字符串