Python 类型错误:int() 参数必须是字符串或数字,而不是“列表”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15624654/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:34:16  来源:igfitidea点击:

TypeError: int() argument must be a string or a number, not 'list'

pythonwhile-loopiterationtypeerror

提问by user2209201

#iterative program to find the highest odd number
m = sorted([a, b, c, d, e, f, g, h, j, k])
n = max(m)
n = int(n)
count = 10
while n%2==0:
    m=[m[:-1]]
    n = max(m)
    n = int(n)
    count =-1
if count==0:
    print 'There are no odd numbers'
else:
    print str(n), 'is the largest odd number'

I input variables that contain an odd number, and it gives me the correct answer, but when I input all even numbers to satisfy the 'count==0'condition, the following error occurs:

我输入包含奇数的变量,它给了我正确的答案,但是当我输入所有偶数以满足'count==0'条件时,出现以下错误:

TypeError: int() argument must be a string or a number, not 'list'

类型错误:int() 参数必须是字符串或数字,而不是“列表”

I can't understand why this error doesn't occur when there are odd numbers input.

我不明白为什么输入奇数时不会发生此错误。

回答by Brandon Humpert

When you have only even numbers, your while loop is generating this error when you've depopulated mcompletely. In that case max(m)returns None, which cannot be the argument of int. To fix this, you need to change your while loop condition to something more correct.

当您只有偶数时,当您m完全减少人口时,您的 while 循环会产生此错误。在那种情况下max(m)返回None,它不能是 的参数int。要解决此问题,您需要将 while 循环条件更改为更正确的内容。

This is, however, not what most would consider "pythonic". Ideally for that you would use a loop more like for n in m[::-1]which traverses min reverse order (or use the reverse=Trueargument of sortedand just for n in m.)

然而,这不是大多数人认为的“pythonic”。理想情况下,您将使用更像是以相反顺序for n in m[::-1]遍历的循环m(或使用and just的reverse=True参数。)sortedfor n in m

回答by abarnert

If you print out what mis inside the loop, this becomes pretty obvious. Or you might want to test it out using an interactive visualizer, or just the debugger.

如果您打印出m循环内的内容,这将变得非常明显。或者您可能想使用交互式可视化器或仅使用调试器来测试它。

Let's say your values are 2, 4, 6, 8, 10, 12, 14, 16, 18, 20. After sorting, you've got:

假设您的价值观是2, 4, 6, 8, 10, 12, 14, 16, 18, 20. 排序后,你有:

m = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
n = max(m) = 20
n = int(n) = 20

This maxis useless, because by the definition of sorting is has to be the last value in the list (and you seem to be relying on that in your loop anyway).

max是没用的,因为根据排序的定义,它必须是列表中的最后一个值(而且你似乎在循环中依赖它)。

And the intis misleading—it makes it look like your code will work even if the numbers are strings instead of numbers, but it actually won't, because sorted(and max) will treat '10'as less than '2', and so on.

并且int是误导性的 - 即使数字是字符串而不是数字,它看起来你的代码也能工作,但实际上不会,因为sorted(和max) 将'10'视为小于'2',依此类推。

But neither of those is your big problem. Because your first nis even, you will go into the loop, and the first thing in the loop is this:

但这些都不是你的大问题。因为你的第一个n是偶数,你将进入循环,循环中的第一件事是:

m=[m[:-1]]

… which will do this:

......这将做到这一点:

m = [[2, 4, 6, 8, 10, 12, 14, 16, 18]]

So, the next two lines do this:

因此,接下来的两行执行此操作:

n = [2, 4, 6, 8, 10, 12, 14, 16, 18] # the max of a 1-element list is that element
n = int([2, 4, 6, 8, 10, 12, 14, 16, 18])

And boom, there's your exception.

繁荣,有你的例外。

If you wanted to set mto all but the last element of m, just do m = m[:-1]. Throwing those extra brackets around it sets mto a listconsisting of one element, which is itself the list consisting of all but the last element of m.

如果您想设置m为除最后一个元素之外的所有元素m,只需执行m = m[:-1]。将那些额外的括号放在它周围设置为由一个元素组成的ma list,它本身就是由除最后一个元素之外的所有元素组成的列表m

Note that, despite what you say in your description, "I input variables that contain an odd number it gives me the correct answer", that isn't true. It only works if your max value is odd, so you never go into the loop in the first place.

请注意,尽管您在描述中说“我输入的变量包含奇数,但它给了我正确的答案”,但事实并非如此。它仅在您的最大值为奇数时才有效,因此您永远不会首先进入循环。

Your code is actually still broken after you fix this, but hopefully now you know how to debug this yourself.

修复此问题后,您的代码实际上仍然损坏,但希望现在您知道如何自己调试了。



Meanwhile, the pythonic way to solve this is to try to translate your high-level English description directly into high-level Python. How do we find the highest odd number in m?

同时,解决这个问题的pythonic方法是尝试将您的高级英语描述直接翻译成高级Python。我们如何在 中找到最高的奇数m

First get the odd numbers in m:

首先得到奇数m

odds = (n for n in m if n % 2)

(This might be more readable if you create an oddfunction—and, if you, you might prefer filterto a generator expression.)

(如果您创建一个odd函数,这可能更具可读性——如果您愿意,您可能更喜欢filter生成器表达式。)

Then, to get the maximum:

然后,要获得最大值:

max_odd = max(odds)

Of course you need to handle the case where there are no odds. You can do that by checking if odd:. But in python, it's usually better to ask forgiveness than permission, so, here's your whole program:

当然你需要处理没有赔率的情况。你可以通过检查来做到这一点if odd:。但是在 python 中,请求宽恕通常比许可更好,所以,这是你的整个程序:

m = [a, b, c, d, e, f, g, h, j, k]
odds = (n for n in m if n % 2)
try:
    print max(odds), 'is the largest odd number'
except ValueError:
    print 'There are no odd numbers'

回答by Rushy Panchal

Your error occurs with the m=[m[::-1]], as @abarnert pointed out.

m=[m[::-1]]正如@abernert 指出的那样,您的错误发生在。

Here's a simple way to find the max odd number in a list:

这是在列表中查找最大奇数的简单方法:

m = sorted([int(n) for n in [a, b, c, d, e, f, g, h, j, k] if n%2==1])
# this makes a list of all ODD integers (converts them from strings)
if len(m) != 0:
    print str(max(m)), 'is the largest odd number'
else:
    print 'No odd integers inputted'

Simplified further into:

进一步简化为:

m = sorted([int(n) for n in [a, b, c, d, e, f, g, h, j, k] if n%2==1])
print (str(max(m)), 'is the largest odd number') if len(m)!=0 else 'No odd integers inputted'