PythonTypeError:';浮动';对象是不可编辑的
我尝试从一个txt文件的向量列表中进行3个切片,共2列,使用条件来分隔RGB 但当我运行程序时,出现以下错误:“'float'对象不可编辑”。有人能帮我吗PythonTypeError:';浮动';对象是不可编辑的,python,python-3.x,Python,Python 3.x,我尝试从一个txt文件的向量列表中进行3个切片,共2列,使用条件来分隔RGB 但当我运行程序时,出现以下错误:“'float'对象不可编辑”。有人能帮我吗 #Conditions B = 0 G = 0 R = 0 for i in range(0,len(vetor_x)): if vetor_x[i] <= 500: vetor_xB[B] = list(vetor_x[i]) vetor_yB[B] = list(vetor_y[i])
#Conditions
B = 0
G = 0
R = 0
for i in range(0,len(vetor_x)):
if vetor_x[i] <= 500:
vetor_xB[B] = list(vetor_x[i])
vetor_yB[B] = list(vetor_y[i])
B += 1
elif vetor_x[i] <= 600:
vetor_xG[G] = list(vetor_x[i])
vetor_yG[G] = list(vetor_y[i])
G += 1
elif vetor_x[i] <= 700:
vetor_xR[R] = list(vetor_x[i])
vetor_yR[R] = list(vetor_y[i])
R += 1
print('####### vetor_xB #######')
print(vetor_xB)
print('####### vetor_yB #######')
print(vetor_xB)
print('####### vetor_xG #######')
print(vetor_xG)
print('####### vetor_yG #######')
print(vetor_yG)
print('####### vetor_xR #######')
print(vetor_xR)
print('####### vetor_yR #######')
print(vetor_yR)
#条件
B=0
G=0
R=0
对于范围(0,len(vetor_x))中的i:
如果您不能将int
或float
类型拆分为列表
你可以先使用str(vetor_x[i])
将它们转换为str
,如果这是你想要的。
list()
将尝试将字符串拆分为每个字符。
例如,list('abc')
将为您提供['a','b','c']
对于int
和float
,这是无法完成的,因为它们不可编辑
看起来您并不打算将您的vetor\x[i]
拆分为每个字符。看起来您只是想将值存储到vetor\xB[B]
中,在这种情况下,您应该创建一个包含那么多None
或0
变量的空列表,然后用您的代码替换它们
for i in range(0,len(vetor_x)):
if vetor_x[i] <= 500:
vetor_xB[B] = vetor_x[i]
vetor_yB[B] = vetor_y[i]
B += 1
.......
范围内的i(0,len(vetor_x)):
如果vetor_x[i]你的向量是什么?看起来像是一个浮点。@KevinFang是范围>=500vetor_x.append(float(x))
why into float?@user5173426内第一列的值列表,因为它们不是integers@Fulana我的意思是,在这段代码之前是什么,你如何定义和初始化它。嗨@ycx,我想把数组分成两部分,通过x坐标(第一列)。例如:400=500、500=600和600=700,并将它们存储在新阵列中。我不知道我是否不理解您的解释,但它仍然犯了一个错误。@Fulana如果是这样,您可以执行vetor\u x.sort()
(如果您希望排序或只是按照原始顺序存储它们,则可以选择),然后直接执行for I in vetor\u x:
循环,检查
for i in range(0,len(vetor_x)):
if vetor_x[i] <= 500:
vetor_xB[B] = vetor_x[i]
vetor_yB[B] = vetor_y[i]
B += 1
.......