我正在尝试将列表转换为元组.
当我谷歌它,我发现很多答案类似于:
l = [4,5,6]
tuple(l)
Run Code Online (Sandbox Code Playgroud)
但如果我这样做,我收到此错误消息:
TypeError:'tuple'对象不可调用
我该如何解决这个问题?
以下代码在Python 2.5和3.0中按预期工作:
a, b, c = (1, 2, 3)
print(a, b, c)
def test():
print(a)
print(b)
print(c) # (A)
#c+=1 # (B)
test()
Run Code Online (Sandbox Code Playgroud)
但是,当我取消注释行(B)时,我得到了UnboundLocalError: 'c' not assigned一行(A).的值a和b被正确地打印.这让我感到困惑,原因有两个:
为什么在行(A)处抛出运行时错误,因为后面的行(B)语句?
为什么变量a和b打印符合预期,同时c引发错误?
我能想到的唯一解释是,赋值创建了一个局部变量,即使在创建局部变量之前,它也优先于"全局"变量.当然,变量在存在之前"窃取"范围是没有意义的.cc+=1c
有人可以解释一下这种行为吗?
我可能在这里有一个脑屁,但我真的无法弄清楚我的代码有什么问题:
for key in tmpDict:
print type(tmpDict[key])
time.sleep(1)
if(type(tmpDict[key])==list):
print 'this is never visible'
break
Run Code Online (Sandbox Code Playgroud)
输出是<type 'list'>但if语句永远不会触发.谁能在这里发现我的错误?
我有一个清单:
list1=[]
Run Code Online (Sandbox Code Playgroud)
列表的长度是未确定的,所以我试图将对象附加到list1的末尾,如下所示:
for i in range(0, n):
list1=list1.append([i])
Run Code Online (Sandbox Code Playgroud)
但是我的输出一直给出这个错误:AttributeError:'NoneType'对象没有属性'append'
这是因为list1作为空列表开始吗?我该如何解决这个错误?
我是Python的新手并且遵循教程.list教程中有一个例子:
example = list('easyhoss')
Run Code Online (Sandbox Code Playgroud)
现在,在教程中,example= ['e','a',...,'s'].但在我的情况下,我得到以下错误:
>>> example = list('easyhoss')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'list' object is not callable
Run Code Online (Sandbox Code Playgroud)
请告诉我我错在哪里.我搜索了这个,但它是不同的.
我set在交互式python会话中使用它作为变量名称而意外地覆盖了 - 有没有什么办法可以在set不重新启动会话的情况下访问原始函数?
(我在那个会话中有很多东西,我宁愿不必这样做,虽然我当然可以在必要的时候.)
我导入tqdm如下:
import tqdm
Run Code Online (Sandbox Code Playgroud)
我使用tqdm来显示我的python3代码的进度,但我有以下错误:
Traceback (most recent call last):
File "process.py", line 15, in <module>
for dir in tqdm(os.listdir(path), desc = 'dirs'):
TypeError: 'module' object is not callable
Run Code Online (Sandbox Code Playgroud)
这是代码:
path = '../dialogs'
dirs = os.listdir(path)
for dir in tqdm(dirs, desc = 'dirs'):
print(dir)
Run Code Online (Sandbox Code Playgroud) 为什么这会给我一个错误?
>>> variable = str(21)
Traceback (most recent call last):
File "<pyshell#101>", line 1, in <module>
variable = str(21)
TypeError: 'str' object is not callable
Run Code Online (Sandbox Code Playgroud) 我经常从我的 Python 代码中得到未捕获的异常(错误),这些异常被描述为TypeErrors. 经过大量的实验和研究,我收集了以下示例(以及细微的变化):
TypeError: func() takes 0 positional arguments but 1 was given
TypeError: func() takes from 1 to 2 positional arguments but 3 were given
TypeError: func() got an unexpected keyword argument 'arg'
TypeError: func() missing 1 required positional argument: 'arg'
TypeError: func() missing 1 required keyword-only argument: 'arg'
TypeError: func() got multiple values for argument 'arg'
TypeError: MyClass() takes no arguments
TypeError: unsupported operand type(s) for +: 'int' and 'str'
TypeError: can only concatenate str …Run Code Online (Sandbox Code Playgroud) 我有一个非常基本的问题.
假设我调用一个函数,例如,
def foo():
x = 'hello world'
Run Code Online (Sandbox Code Playgroud)
如何让函数以这样的方式返回x,我可以将它用作另一个函数的输入或者在程序体内使用变量?
当我使用return并在另一个函数中调用该变量时,我得到一个NameError.