Python 数学 - 类型错误:“NoneType”对象不可订阅

发布于 2025-01-06 09:18:59 字数 862 浏览 1 评论 0原文

我正在为数学编写一个小程序(没有特殊原因,只是想这样做),并且遇到了错误“TypeError:‘NoneType’对象不可下标。

我以前从未见过此错误,所以我不知道 是什么意思:

import math

print("The format you should consider:")
print str("value 1a")+str(" + ")+str("value 2")+str(" = ")+str("value 3a ")+str("value 4")+str("\n")

print("Do not include the letters in the input, it automatically adds them")

v1 = input("Value 1: ")
v2 = input("Value 2: ")
v3 = input("Value 3: ")
v4 = input("Value 4: ")

lista = [v1, v3]
lista = list.sort(lista)

a = lista[1] - lista[0]

list = [v2, v4]
list = list.sort(list)

b = list[1] = list[0]

print str(a)+str("a")+str(" = ")+str(b)

错误

Traceback (most recent call last):
  File "C:/Users/Nathan/Documents/Python/New thing", line 16, in <module>
    a = lista[1] - lista[0]
TypeError: 'NoneType' object is not subscriptable

I'm making a small program for math (no particular reason, just kind of wanted to) and I ran into the error "TypeError: 'NoneType' object is not subscriptable.

I have never before seen this error, so I have no idea what it means.

import math

print("The format you should consider:")
print str("value 1a")+str(" + ")+str("value 2")+str(" = ")+str("value 3a ")+str("value 4")+str("\n")

print("Do not include the letters in the input, it automatically adds them")

v1 = input("Value 1: ")
v2 = input("Value 2: ")
v3 = input("Value 3: ")
v4 = input("Value 4: ")

lista = [v1, v3]
lista = list.sort(lista)

a = lista[1] - lista[0]

list = [v2, v4]
list = list.sort(list)

b = list[1] = list[0]

print str(a)+str("a")+str(" = ")+str(b)

The error:

Traceback (most recent call last):
  File "C:/Users/Nathan/Documents/Python/New thing", line 16, in <module>
    a = lista[1] - lista[0]
TypeError: 'NoneType' object is not subscriptable

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

左岸枫 2025-01-13 09:18:59
lista = list.sort(lista)

这应该是

lista.sort()

.sort() 方法就地,并且返回 None。如果你想要一些不就地的东西,它返回一个值,你可以使用

sorted_list = sorted(lista)

旁白#1:请不要调用你的列表list。这会破坏内置列表类型。

除了#2:我不确定这行代码的用途是什么:

print str("value 1a")+str(" + ")+str("value 2")+str(" = ")+str("value 3a ")+str("value 4")+str("\n")

它简单吗

print "value 1a + value 2 = value 3a value 4"

?换句话说,我不知道为什么你要对已经是 str 的东西调用 str 。

除了 #3:有时你使用 print("something") (Python 3 语法),有时你使用 print "something" (Python 2)。后者会在 py3 中给你一个 SyntaxError,所以你必须运行 2.*,在这种情况下你可能不想养成这个习惯,否则你将最终打印带有额外括号的元组。我承认它在这里会工作得很好,因为如果括号中只有一个元素,它不会被解释为元组,但对于 python 的眼睛来说它看起来很奇怪。

lista = list.sort(lista)

This should be

lista.sort()

The .sort() method is in-place, and returns None. If you want something not in-place, which returns a value, you could use

sorted_list = sorted(lista)

Aside #1: please don't call your lists list. That clobbers the builtin list type.

Aside #2: I'm not sure what this line is meant to do:

print str("value 1a")+str(" + ")+str("value 2")+str(" = ")+str("value 3a ")+str("value 4")+str("\n")

is it simply

print "value 1a + value 2 = value 3a value 4"

? In other words, I don't know why you're calling str on things which are already str.

Aside #3: sometimes you use print("something") (Python 3 syntax) and sometimes you use print "something" (Python 2). The latter would give you a SyntaxError in py3, so you must be running 2.*, in which case you probably don't want to get in the habit or you'll wind up printing tuples, with extra parentheses. I admit that it'll work well enough here, because if there's only one element in the parentheses it's not interpreted as a tuple, but it looks strange to the pythonic eye..

找个人就嫁了吧 2025-01-13 09:18:59

发生 TypeError: 'NoneType' object is not subscriptable 异常是因为 lista 的值实际上是 None。如果您在 Python 命令行中尝试执行此操作,则可以重现代码中出现的 TypeError

None[0]

lista 设置为 None 的原因是因为 的返回值>list.sort()None...它返回原始列表的排序副本。相反,正如文档指出的,列表会就地排序 而不是制作副本(这是出于效率原因)。

如果您不想更改原始版本,可以使用

other_list = sorted(lista)

The exception TypeError: 'NoneType' object is not subscriptable happens because the value of lista is actually None. You can reproduce TypeError that you get in your code if you try this at the Python command line:

None[0]

The reason that lista gets set to None is because the return value of list.sort() is None... it does not return a sorted copy of the original list. Instead, as the documentation points out, the list gets sorted in-place instead of a copy being made (this is for efficiency reasons).

If you do not want to alter the original version you can use

other_list = sorted(lista)
少女的英雄梦 2025-01-13 09:18:59

在此链接 https://docs.python.org/2/tutorial/datastructs.html 你可以阅读这个方法
“就地对列表中的项目进行排序”这意味着结果值将进行排序并
结果将由其自身决定。该函数返回 None。

当您将结果分配给第 14 行中的“lista”时,

lista = list.sort(lista)

您可以将其设置为 None。这就是错误。 None 总是没有数据并且不能
可订阅。 “TypeError: 'NoneType' object is not subscriptable”

要更正此错误(用于对列表进行排序),请在第 14 行执行此操作:

lista.sort() # this will sort the list in line

但还有一些其他错误:
在第 18 行中,当您分配时:

list = [v2, v4]

您 clob 这个内置类型“list”,您将收到以下错误:

TypeError: 'list' object is not callable

要更正此操作,请说:

lista2 = [v2, v4]

在第 19 行中再次出现与第 14 行相同的错误。执行此操作以对其他列表进行排序:

lista2.sort()

在第 21 行中,您尝试为内置类型列表建立索引。要纠正此问题,请执行以下操作:

b = lista2[1] = lista2[0]

这样您的代码就可以正常运行。最后是完整的正确代码:

import math

print("The format you should consider:")
print str("value 1a")+str(" + ")+str("value 2")+str(" = ")+str("value 3a ")+str("value 4")+str("\n")

print("Do not include the letters in the input, it automatically adds them")

v1 = input("Value 1: ")
v2 = input("Value 2: ")
v3 = input("Value 3: ")
v4 = input("Value 4: ")

lista = [v1, v3]
lista.sort()

a = lista[1] - lista[0]

lista2 = [v2, v4]
lista2.sort()

b = lista2[1] = lista2[0]

print str(a)+str("a")+str(" = ")+str(b)

At this link https://docs.python.org/2/tutorial/datastructures.html you can read this method
"Sort the items of the list in place" this means that the result value will on sorted and
the result will be on itself. The function returns None.

When you assign the result to "lista" in line 14

lista = list.sort(lista)

you area setting it to None. That is the error. None always has no data and can not be
subscriptable. "TypeError: 'NoneType' object is not subscriptable"

to correct this error (for sort the list) do this at line 14:

lista.sort() # this will sort the list in line

But there are some other errors:
in line 18 when you assign:

list = [v2, v4]

You clob this built in type "list" and you will get the following error:

TypeError: 'list' object is not callable

To correct this do that, say:

lista2 = [v2, v4]

Again in line 19 the same error of line 14. Do this to sort the other list:

lista2.sort()

In line 21 you are trying to index the built in type list. To correct do this:

b = lista2[1] = lista2[0]

With this your code will run fine. Finally the whole correct code:

import math

print("The format you should consider:")
print str("value 1a")+str(" + ")+str("value 2")+str(" = ")+str("value 3a ")+str("value 4")+str("\n")

print("Do not include the letters in the input, it automatically adds them")

v1 = input("Value 1: ")
v2 = input("Value 2: ")
v3 = input("Value 3: ")
v4 = input("Value 4: ")

lista = [v1, v3]
lista.sort()

a = lista[1] - lista[0]

lista2 = [v2, v4]
lista2.sort()

b = lista2[1] = lista2[0]

print str(a)+str("a")+str(" = ")+str(b)
绝影如岚 2025-01-13 09:18:59

正如前面在答案之一中所说,当列表的值为空时,就会发生此错误。好吧,虽然与这个问题不完全相关,但在使用 opencv 和 numpy 读取图像时,我也发生了同样的错误,因为发现文件名可能与指定的文件名不同,或者因为工作目录没有正确指定。

As previously said in one of the answers this error occurs when value of the list turns out to be empty. Well though not fully relevant to this question, the same error occured for me while reading images using opencv and numpy because the file name was found to be different than that specified probably or because the working directory has not been specified properly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文