简体   繁体   English

类型错误:“int”对象不支持项目分配

[英]TypeError: 'int' object does not support item assignment

p=range(0,1200,10)
lenp=len(p)
rp=1.331
po=1000
T=280
Temp=(lenp)

for i in p:
    Temp[i]=T*(p[i]/po)**rp
print T

Im getting this error and don't know how to fix it...我收到这个错误,不知道如何解决它...

Temp[i]=T*(p[i]/po)**rp

TypeError: 'int' object does not support item assignment

You are presumably trying to build a list of length lenp here.您可能正在尝试在此处构建长度为lenp的列表。 You'd need to create a list by multiplication here:您需要在此处通过乘法创建一个列表:

Temp = [None] * lenp

but you'd be better off building the list by appending to it:但是你最好通过附加到它来构建列表:

Temp = []
for i in p:
    Temp.append(T * (i / po) ** rp)

where you don't use p[i] but i directly ;你不使用p[i]直接使用i Python for loops are for each loops really. Python for循环实际上是针对每个循环的。

Your range() produces values in the series [0, 10, 20, ... 1200) and the for loop assigns each of those values to i per iteration.您的range()产生系列[0, 10, 20, ... 1200)中的值,并且for循环每次迭代将这些值中的每一个分配给i If you use i to index into p again you'd run into problems;如果您再次使用i索引p ,则会遇到问题; p[0] would still be 0 , but p[10] would then be 100 , p[20] is 200 , etc. until p[120] throws an IndexError because there are only 119 different values in that range. p[0]仍然是0 ,但p[10]将是100p[20]200等等,直到p[120]抛出一个IndexError因为该范围内只有 119 个不同的值。

You can collapse the for loop appending to Temp into a list comprehension to build the list in one go:您可以将附加到Tempfor循环折叠到列表理解中,以一次性构建列表:

rp=1.331
po=1000
T=280

Temp = [T * (i / po) ** rp for i in range(0, 1200, 10)]

There are a couple of issues in your code.您的代码中有几个问题。

The most immediate one is that your Temp value is not a sequence that you can assign things to, just an integer (the parentheses don't do anything).最直接的是你的Temp值不是一个你可以赋值的序列,只是一个整数(括号没有任何作用)。 While you could make it into a tuple by adding a comma, that still won't let you assign values into Temp after it has been created (tuples are immutable).虽然您可以通过添加逗号将它变成一个元组,但在创建Temp后仍然不允许您将值分配给它(元组是不可变的)。 Instead, you probably want a list.相反,您可能想要一个列表。

However, there's another issue with your loop.但是,您的循环还有另一个问题。 A for loop like for value in sequence assigns values from the sequence to the variable value .类似于for value in sequence for 循环将sequence中的value分配给变量value It doesn't assign indexes!它不分配索引! If you need indexes, you can either use enumerate or use a different looping construct, such as a list comprehension.如果您需要索引,您可以使用enumerate或使用不同的循环结构,例如列表推导式。

Here's a minimally changed version of your code that first creates a list of lenp zeros, then replaces their values with the computed ones:这是您的代码的最小更改版本,它首先创建一个lenp零列表,然后用计算出的值替换它们的值:

p=range(0,1200,10)
lenp=len(p)
rp=1.331
po=1000
T=280
Temp=[0]*lenp

for i, p_value in enumerate(p):
    Temp[i]=T*(p_value/po)**rp
print T

Here's a more pythonic version that uses a list comprehension instead of an ordinary loop:这是一个更pythonic的版本,它使用列表理解而不是普通循环:

rp=1.331
po=1000
T=280
Temp = [T*(p_value/po)**rp for p_value in range(0,1200,10)]

Your Temp is not a tuple, it's just an integer in parenthesis.您的Temp不是元组,它只是括号中的整数。 You can fix this with a simple , .您可以使用简单的,来解决此问题。 Consider the following:考虑以下:

>>> x = (42)  # not a tuple
>>> x[0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object has no attribute '__getitem__'
>>> 
>>> x = (42,)  # tuple
>>> x[0]
42

However , tuples are immutable, and you cannot perform such an assignment:但是,元组是不可变的,您不能执行这样的赋值:

>>> x[0] = 0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment

You can fix this issue by using a list instead.您可以改用列表来解决此问题。

The mistake is that when we assign a (number) to x, this is what is actually happening,错误在于,当我们为 x 分配一个(数字)时,这就是实际发生的情况,

>>> x = (50)
>>> x
50

So if you want to create an empty list , you can do it this way所以如果你想创建一个空列表,你可以这样做

rows = 5
cols = 3
matrix = [[0 for c in range(cols)] for r in range(rows)]

Its gonna create something like this one,它会创造出这样的东西,

>>> matrix
[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM