简单的问题。
我有一个数字(整数):
mynumber = 1239
我想把它转换成一个单独的整数列表,比如[1,2,3,9]
numbersplit = [int(x) for x in mynumber]
但是我得到了一个错误:
TypeError: 'int' object is not iterable
为什么这个不起作用?我只是确保ints是真正的ints。
但是,当我将它包装在str中时,它可以正常工作吗?
[int(x) for x in str(mynumber)]
[1, 2, 3, 9, 8, 9, 8, 7, 2, 3, 9, 4, 7, 8]
是否需
n,m,k=map(int, input().split())
data=list(map(int, input().split()))
for i in data:
print(i)
#consequence=2 3 4 5 6
for i in data:
print(max(i))
#consequence= TypeError:'int‘对象不可迭代
如果int是可迭代的,那么为什么我可以在for循环中使用int?为什么在这种情况下我不能使用max()函数?
我不知道为什么它会给我这个错误,我通常这样做,它工作。这是它第一次做这样的事情。
这是我的错误
TypeError: '_io.TextIOWrapper' object is not subscriptable
这是我的代码
import time; import os
l = 0
while True:
e = open("your code", "r")
r = e.readlines()
e.close
l+=1
if l == 1:
l = 0
if e[l] == "help}0{"
我有一个zip文件,其中包含一个文本文件(数百万行)。我需要逐行读取,对每一行应用一些转换,然后写入一个新文件并将其压缩。
with zipfile.ZipFile("orginal.zip") as zf, zipfile.ZipFile("new.zip", "w") as new_zip:
with io.TextIOWrapper(zf.open("orginal_file.txt"), encoding="UTF-8") as fp, open("new.txt",
我写了一些代码:
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)
我意外地看到,这会导致V8 (Chrome、Node.js等)出现错误:
for (let val of Symbol()) { /*...*/ }
TypeError:符号不是一个函数,或者它的返回值不可迭代
似乎任何其他不可迭代的值(包括一个函数)都会导致另一个错误:
for (let val of function () { throw 'never called' }) { /*...*/ }
TypeError:(中间值)不可迭代
正如所述,错误是特定于Chrome的:
TypeError:'x‘不是一个函数,或者它的返回值不可迭代(Chro
为什么这个代码:
let a = {};
let m = new Map(a);
给我一个错误:
code.0.0.128.js:33943 Uncaught TypeError: undefined is not a function
at new Map (native)
at http://localhost:/code.0.0.128.js:33943:26
at exports.default (http://localhost:/code.0.0.128.js:34006:7)
at combination (http://localhost:/co
这是我对的回答的结果。
为什么我会得到如下所述的TypeError?
枚举是什么情况?
我以为
for k, v in enumerate(iterable)`
相当于:
for k in iterable:
v = iterable[k]
显然不是。或者至少,在所有情况下,这都是不一样的。
..。季节=“春天”、“夏天”、“秋天”、“冬天”。列表(列举(季节)) (0,‘春天’),(1,‘夏天’),(2,‘秋季’),(3,‘冬天’).列表(列举(季节,start=1)) (1,‘春天’),(2,‘夏天’),(3,‘秋天’,(4,‘冬天’)
我当时就认为,en
在做了几年的C代码之后,我又回到了Python,在训练自己恢复Python编码习惯时,我有点困惑。
我试着运行这个非常简单的小代码,但是我一直得到一个TypeError,如标题中所描述的。我已经搜索了很多,但不知道这有什么问题:
def toLower(pStr):
i = 0
for i in pStr:
if ord(pStr[i]) >= 65 and ord(pStr[i]) <= 90:
pStr[i] = chr(ord(pStr[i])+28)
return pStr
testStr = "TEST STRING"
p
当我调用getLink()时,我有时会得到这个错误(TypeError:'NoneType‘对象是不可订阅的),有时我不会。有人知道我为什么会得到这个错误吗?getLink()应该是可订阅的... def getLink():
for link in getElements():
if "orgs" in link:
return ['orgs', link]
elif "pac" in link and "summary"
当循环第二次启动时,这会产生一个错误:
33 for i in range(1,len(t)):
---> 34 aux=t[i][2]
35 aux2=t[i][4]
36 empresa=t[i][0] TypeError: 'int' object is not subscriptable
for i in range(1,len(t)):
aux=t[i][2]
aux2=t[i][4]
empresa=t[i][0]
fecha=t[i][1]
mails=aux.split('
所以,我从下面的代码开始:
x = [1, [2, 3]]
y = [4, [5, 6]]
z = [7, [8, 9]]
amazing_list = [x, y, z]
我对它做了一个简单的列表理解
print [l for l in amazing_list]
按预期打印[[1, [2, 3]], [4, [5, 6]], [7, [8, 9]]]
print [l for i, l in amazing_list]
按预期打印[[2, 3], [5, 6], [8, 9]]
print [a for i, l in amazing_list for a in l]
按预期打印[2,
见到你很高兴。这是我第一次问问题。我很高兴我找到了这个社区。 我正在学习javascript,并编写了这个函数,但错误总是出现。错误提示:Uncaught TypeError: Cannot set property 'id' of undefined,但我不知道为什么!toDos是一个包含文本,id的数组。当我将a更改为某个随机数时,错误不会出现。为什么不能使用参数访问toDos.id? function resetId() {
let a = 0;
while (a !== toDos.length) {
toDos[a - 1].id = a;
我知道...在JavaScript中的传播操作符。我们可以在数组和对象上使用它:
let user = {
name: "aman",
rollno: 11
}
let newobj1 = {...user}
let newobj2 = [...user]
console.log(newobj1)
console.log(newobj2)
为什么newobj2会给出一个错误,TypeError: user is not iterable,但是newobj1工作得很好?
我在学Python。在使用Python3.6.0REPL时,我遇到了以下异常结果。
a = [1, 2, 3]
for a in a :
print(a)
产出:
1
2
3
再来一次。
产出:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
我能看到'a‘的价值。
>>>a
3
它可以工作其他迭代器,如i。
我不知道如何搜索谷歌的
这是我的代码:
function permAlone(string) {
if (string.length < 2) return string; // This is our break condition
var permutations = []; // This array will hold our permutations
for (var i = 0; i < string.length; i++) {
var character = string[i];
// Cause we don't want any dupli
因此,我编写了一个函数,它读取一个文件(csv),并返回每一行数字的平均值,作为一个列表。我认为代码应该能工作,但是我已经尝试了很多方法,无法将数据转换成数值(然后可以进行求和等等)。从名单上。
def line_averages(filename):
file = open(filename, 'r')
data = file.readlines()
file.close()
x = []
for i in data:
data_splitted = i.split()
line_average = su
我正在尝试从中抓取数据,但遇到了一些困难。我在过去通过网络抓取创建csvs时也设置了一些类似的程序,但这个程序给我带来了一些麻烦。我一直在一个特定的for循环中得到一个TypeError,但我有一个本质上相同代码的不同程序工作得很好。我确信有一个简单的方法可以解决这个问题,但我一直找不到。
我已经尝试了Pycharm给出的建议修复,但它们都没有修复任何问题。通过将变量更改为object类型,它说明None类型的对象也不能迭代。
for tr in rows:
cells = tr.find("td")
temp = []
for td in cells:
例如,如果我这样做的话,我想了解list +='a'和list=list+'a'之间的区别:
li=[]
for i in 'string':
li+=i
我没有错误,但是如果我使用第二个选项,按照惯例,这个选项应该是相同的,但是会产生错误。
li=[]
for i in 'string':
li=li+i
TypeError:只能将列表(不是"str")连接到列表中
正确的方法应该是这样的:
li=[]
for i in 'string':
li=li+[i]
我想
我是编程新手。我对Java中的字符串有一个误解。我知道字符串在java中是不可变的。这意味着它不能改变,但我见过很多读取文件逐个字符串的例子,currentString总是在每次迭代中被改变。请帮助我理解为什么这是可能的,并写下来。URL 中的示例
BufferedReader br = null;
try {
String sCurrentLine;
br = new BufferedReader(new FileReader("C:\\testing.txt"));//file name with path
while ((sCurrentLine
在Python3.6.1中,我尝试将tempfile.SpooledTemporaryFile包装在io.TextIOWrapper中:
with tempfile.SpooledTemporaryFile() as tfh:
do_some_download(tfh)
tfh.seek(0)
wrapper = io.TextIOWrapper(tfh, encoding='utf-8')
yield from do_some_text_formatting(wrapper)
wrapper = io.TextIOWrapper(tfh, en
我试图用在Python2中工作的代码在Python3中打开一个pickle文件,但现在给我一个错误。代码如下:
with open(file, 'r') as f:
d = pickle.load(f)
TypeError Traceback (most recent call last)
<ipython-input-25-38f711abef06> in <module>()
1 with open(file, 'r') as f:
---->
下面的代码是错误的,使用openpyxl和我不知道为什么:
Zip = zip_code.group(0)[:6]
title_column_name = "Member Address Line 3"
ws = config.workbook.active
for row in ws.rows[1:]: # errors out here
if row[1].value.find(Zip) != -1:
既然range对象是可订阅的,为什么itertools.repeat对象不能订阅?
In [10]: range(3, 10)[3]
Out[10]: 6
In [11]: import itertools as it
In [12]: it.repeat(5)[3]
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython
wcyear = 2000
def worldcup():
global wcyear
for i in wcyear:
wcyear = wcyear+4
print wcyear
print 'world cup year: ',wcyear
worldcup()
print 'Out of function wc year: ',wcyear
我得到的错误是:TypeError: 'int' object is not iterable是全局变量,循环中不支持吗?
在python中,我得到了这个错误:
TypeError: 'int' object is unsubscriptable
这发生在代码行上:
sectorcalc[i][2]= ((today[2]/yesterday[2])-1)
我在任何地方都找不到一个好的定义来定义python的不可订阅。
for quote in sector[singlestock]:
i+=1
if i < len(sector):
if i==0:
sectorcalc[i][0]= quote[0]
我是Python编程新手,最近我尝试读取csv文件,然后将该csv文件中的数据保存到文本文件中。这是我正在使用的代码
csv_file = open('example.csv', 'r')
txt_file = open('example.txt', 'w')
with open(txt_file, 'w') as my_output_file:
with open(csv_file, 'r') as my_input_file:
[my_output_file.write