sum = []
def s(d):
d = list[int]
sum = []
for i in d:
sum += i
return(sum)
s([1, 2, 3])
Python给了我以下消息:TypeError: 'type' object is not iterable.
我如何使代码工作?
我正在尝试找出从模块中检索的方法的参数。我发现了一个具有方便函数getargspec的inspect模块。它适用于我定义的函数,但不适用于来自导入模块的函数。
import math, inspect
def foobar(a,b=11): pass
inspect.getargspec(foobar) # this works
inspect.getargspec(math.sin) # this doesn't
我会得到如下的错误:
File "C:\...\Python 2.5\Lib\inspect.py", line 743, in getargspe
我意外地看到,这会导致V8 (Chrome、Node.js等)出现错误:
for (let val of Symbol()) { /*...*/ }
TypeError:符号不是一个函数,或者它的返回值不可迭代
似乎任何其他不可迭代的值(包括一个函数)都会导致另一个错误:
for (let val of function () { throw 'never called' }) { /*...*/ }
TypeError:(中间值)不可迭代
正如所述,错误是特定于Chrome的:
TypeError:'x‘不是一个函数,或者它的返回值不可迭代(Chro
我正在尝试将类型添加到返回生成器的方法中。每当我使用指定的返回类型运行此程序时,都会引发TypeError。
添加引号或删除输入可以修复错误,但这看起来像是hack。当然,有一种正确的方法可以做到这一点。
def inbox_files(self) -> "Generator[RecordsFile]":
...
# OR
def inbox_files(self):
...
from typing import Generator, List
from .records_file import RecordsFile
Class Marshalle
在Python3中,我试图找出关于减()和函数作为函数参数的方法,或者更好地将一个函数作为另一个函数的参数传递,如果第一个参数不是显式的,请参见下面的
给予:
# define a function `call` where you provide the function and the arguments
def call(y,f):
return f(y)
# define a function that returns the square
square = lambda x : x*x
# define a function that returns the increm
将None传递给Python的list构造函数是一个TypeError
>>> l = list(None)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not iterable
但是,使用方括号实例化list是很好的;在内置函数中使用None也很好:
>>> l = [None]
>>> l.append(None)
&
我需要根据字典的值从字典中提取密钥。
我想要获取的键,其值在下面的dict值1。
>>> a = {'random': 0, 'fixed': 1}
>>> for k, v in a.items():
... if 1 in v:
... print k
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
TypeError: argument of type 'int
请你解释一下,它为什么不能正常工作?
print([sum(a)**2 for a in range(100)] - [sum(a*a) for a in range(100)])
我在努力解决
一条龙
Traceback (most recent call last):
File "C:/Users/1/PycharmProjects/Python_DevOps/hw/hw9.py", line 15
print([sum(a)**2 for a in range(1,100)] - [sum(a*a) for a in range(1,100)])
Fil
我在windows7上使用python 3.2.2,我正在尝试创建一个程序,它接受7个数字,然后告诉用户有多少是正数,有多少是负数,有多少是零。这是我到目前为止所得到的:
count=7
for i in count:
num = float(input("Type a number, any number:"))
if num == 0:
zero+=1
elif num > 0:
positive+=1
elif num < 0:
negative+=1
print (positi
上的iter函数示例
with open("mydata.txt") as fp:
for line in iter(fp.readline):
print line
给了我这个:
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
TypeError: 'builtin_function_or_method' object is not iterable
这怎么可能呢?(Python 2.6.4)
因此,我在python中编写了这个简单的程序,它的函数名为num,函数以两个参数作为整数,并检查其中是否存在"20“,如果是,则返回True,否则返回False,您会注意到,在if语句中,我调用了另一个函数sum,它是python中内置的函数,所以当我试图传递函数中的a和b的值而不将它们作为元组传递时,它会给出下面提到的错误。所以我的问题是,当我像这个sum((a,b))而不是这个sum(a,b)传递它们时,它为什么会工作呢?
File "c.py", line 8, in <module>
print(num(220,10))
File
我写了一些代码:
def ICP(x):
numofrepeat=0
warning=0
while numofrepeat<len(str(x)) or (x[numofrepeat]==2) or (x[numofrepeat]==3) or (x[numofrepeat]==5) or (x[numofrepeat]==7):
if (x[numofrepeat]==0):
warning=warning+1
if warning>1:
numofrepeat=len(x)
一个关于偶像Python的问题。假设我有一个函数:
def a_function(list_of_things):
for item in list_of_things:
process_item(item)
现在假设我很难确定输入参数是一个列表。即使是现在,我也能听到甜蜜的毕多诺大合唱恳求我:“使用鸭-键入和例外!”
def a_function(list_of_things):
try:
for item in list_of_things:
process_item(item)
except:
我试着解决Python的10.3练习
编写一个函数,该函数接受一个数字列表并返回累积和;也就是说,一个新的列表,其中ith元素是原始列表中第一个i + 1元素的和。例如,[1, 2, 3]的累积和是[1, 3, 6]。
我得到了一个包含以下代码的TypeError:
def culm_sum(num):
res= []
a = 0
for i in num:
a += i
res += a
return res
当我给culm_sum([1, 2, 3])打电话的时候
TypeError: 'int' ob
因此,我尝试使用线程模块,以了解它的基础知识,当我运行我的代码时,我得到一个错误,没有清楚地说明我做错了什么。下面是代码,你可以在下面找到错误日志:
import threading
import time
start = time.perf_counter()
def square(y):
print('Starting processing')
for x in range(y):
i = x * x
print(f'Done processing {i}')
threads = []
# creating
我只是试着用pyautogui做一个简单的自动递增数字类型,不知道我在这里做错了什么,对不起,这类东西不是很有经验。
import pyautogui as pg
import time
time.sleep(10)
def type():
pg.typewrite(val)
pg.press('enter')
time.sleep(2)
val = 11000
while(val!=30000):
type()
val=val+1
TypeError: 'int' object is not iterable
如前
我正在尝试测试Python中装饰器的实用程序。当我编写以下代码时,会出现一个错误:
TypeError: fizz_buzz_or_number() takes 1 positional argument but 2 were given
我首先将函数log_calls(fn)定义为
def log_calls(fn):
''' Wraps fn in a function named "inner" that writes
the arguments and return value to logfile.log ''&
error TypeError: 'NoneType' object is not iterable是什么意思?
我在这段Python代码中得到了它:
def write_file(data, filename): # creates file and writes list to it
with open(filename, 'wb') as outfile:
writer = csv.writer(outfile)
for row in data: # ABOVE ERROR IS THROWN HERE
writer.wri
我试图在python中读取具有多处理的文件。下面是一个小例子:
import multiprocessing
from time import *
class class1():
def function(self, datasheetname):
#here i start reading my datasheet
if __name__ == '__main__':
#Test with multiprosessing
pool = multiprocessing.Pool(processes=4)
pool.map(cl
我正在尝试获取对象的第n个前置对象:
u = G.predecessors(v)[0]
但这将在Python 2上返回以下错误:
TypeError: 'dict_keyiterator' object is not subscriptable
在Python 3上:
TypeError: 'dict_keys' object is not subscriptable
我该怎么解决这个问题呢?
我从Python文档中读到了对象的内置迭代方法:
它对__iter__方法的说明如下:
返回迭代器对象。对象需要支持下面描述的迭代器协议。
但是,在我看来似乎不是这样的,如果我让__iter__方法返回一个像'abc'那样的字符串,我会得到一个类型错误:TypeError: iter() returned non-iterator of type 'str'但是我们知道,字符串是可迭代的对象,为什么python会把它当作不是迭代对象呢?
我可以像这样解决这个问题:
def __iter__(self):
return 'abc'
即使集合是不可接受的,在其他集合中进行成员资格检查也是可行的:
>>> set() in {frozenset()}
True
我期望TypeError: unhashable type: 'set'与Python中的其他行为保持一致:
>>> set() in {} # doesn't work when checking in dict
TypeError: unhashable type: 'set'
>>> {} in {frozenset()} # looking up some oth