Skip to main content
 首页 » 编程设计

python之错误反转():list is not callable

2025年05月04日14davidwang456

<分区>

def dictrev(): 
    prompt = "Enter the key and value of a dictionary like this: 'key:value':" 
    list = [];list_rev = [];i = 0 
    while True: 
        element = raw_input(prompt).strip().split(':') 
        if element == ['.']: 
            break 
        list.append([]);list_rev.append([]) 
        print element 
        print list(reversed(element)) 
        list[i] = element 
        list_rev[i] = list(reversed(element))   
        i += 1 
    print 'The dictionary you input is:' 
    print dict((list)) 
    print '\nThe dictionary whose key and value are reversed is:' 
    print dict((list_rev)) 
 
dictrev() 

这是 Core Python Programming 7-7 题的代码。

但是,当我运行该程序时,我在代码中得到了一个TypeError: 'list' object is not callable

打印列表(反转(元素))

我所做的如下:

  1. 在 Stackoverflow 和 google 中搜索“list is not callable”主题,但我没有找到方法;

  2. goole list() 和 reversed() 的方法,但我不认为我的代码有什么问题

  3. 打印可变元素、element_reversed 和元素类型。

  4. 像这样在另一个程序中单独获取代码:

l = ['a','b']

打印 l

打印列表(反转(l))

没有像“列表对象不可调用”这样的错误。我很纳闷!!!

我已经做了我能做的来解决这个问题。感谢大家的帮助。