值错误: max()参数为空序列 [python 3]

2024-05-04 19:14:28 发布

您现在位置:Python中文网/ 问答频道 /正文

计算和后列表是否为空??

t = int(input())
while t > 0:
    n,m=map(int,input().split())
    a=map(int,input().split())
    l,o=sum(a),max(a)
    print(l)
    if ((o * n)-l) == m:
        print("YES")
    else:
        print((o * n)-l)
        print("NO")
    t = t-1

Tags: nomap列表inputifelsemaxyes
2条回答

在Python 3中,map()返回一个迭代器(与Python 2不同)。sum()函数已经遍历了所有结果,迭代器现在为空:

>>> sample = '1 2 3'
>>> map(int, sample.split())
<map object at 0x10523b2e8>
>>> a = map(int, sample.split())
>>> list(a)
[1, 2, 3]
>>> list(a)
[]

使用list()将结果复制到列表对象,或者使用列表理解而不是map()

a = [int(i) for i in input().split()]

像这样试试:

inputparam='100 2 300'
a = [int(i) for i in inputparam.split()]
maxNo=max(a)
sumno=sum(a)

相关问题 更多 >