Riv*_*ain 1 python list typeerror
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()
Run Code Online (Sandbox Code Playgroud)
这是Core Python Programming 7-7 中的问题代码.
但是,当我运行程序时,我得到一个TypeError:'list'对象在代码中是不可调用的
print list(reversed(element))
我所做的如下:
在Stackoverflow和google中搜索'list is not callable'主题,但我没有得到该方法;
goole list()和reverse()的方法,但我认为我的代码没有任何问题
打印变量元素,element_reversed和元素的类型.
将代码单独放在另一个程序中,如下所示:
l = ['a','b']
print l
print list(reversed(l))
像'list object不可调用'一样没有错.我很困惑!
我已经做了我能做的事来解决这个问题.谢谢大家的帮助.
| 归档时间: |
|
| 查看次数: |
307 次 |
| 最近记录: |