Python 忽略sum()中的str
我有一个列表,其中主要包含数值。我需要找到最高的值,确定它所包含的列表的索引,并汇总该列表中的所有值 我有一个很好用的代码:Python 忽略sum()中的str,python,python-3.x,Python,Python 3.x,我有一个列表,其中主要包含数值。我需要找到最高的值,确定它所包含的列表的索引,并汇总该列表中的所有值 我有一个很好用的代码: results = [[213, 124, 100], [123.7, 444.6, 111, 12], [], [22, 11, 100, 2], [-1000]] highest, row, total = -1, 0, 0 for a, b in enumerate(resul
results = [[213, 124, 100],
[123.7, 444.6, 111, 12],
[],
[22, 11, 100, 2],
[-1000]]
highest, row, total = -1, 0, 0
for a, b in enumerate(results):
for i in b:
if i > highest:
highest = i
row = a
total = sum(b)
问题是,我需要确保,如果其中一个值是字符串,则算法不会中断。
有没有一种方法可以忽略sum中的字符串,或者我也需要遍历b并检查字符串?您已经遍历了
b
;只需检查每个值不是字符串即可。但是,是的,当使用sum
时,也需要过滤掉字符串值,请使用生成器表达式:
for a, b in enumerate(results):
for i in b:
if not isinstance(i, str) and i > highest:
highest = i
row = a
total = sum(i for i in b if not isinstance(i, str))
您可以反转测试并确保值为int或float:
for a, b in enumerate(results):
for i in b:
if isinstance(i, (int, float)) and i > highest:
highest = i
row = a
total = sum(i for i in b if isinstance(i, (int, float)))
如果你能确保列表只包含前面的数字,那就更好了。但是:
highest, row, total = 0, 0, 0
filtered_results = [[i for i in b if isinstance(i, (int, float))] for b in results]
for a, b in enumerate(filtered_results):
for i in b:
if i > highest:
highest = i
row = a
total = sum(b)
您已经在迭代
b
;只需检查每个值不是字符串即可。但是,是的,当使用sum
时,也需要过滤掉字符串值,请使用生成器表达式:
for a, b in enumerate(results):
for i in b:
if not isinstance(i, str) and i > highest:
highest = i
row = a
total = sum(i for i in b if not isinstance(i, str))
您可以反转测试并确保值为int或float:
for a, b in enumerate(results):
for i in b:
if isinstance(i, (int, float)) and i > highest:
highest = i
row = a
total = sum(i for i in b if isinstance(i, (int, float)))
如果你能确保列表只包含前面的数字,那就更好了。但是:
highest, row, total = 0, 0, 0
filtered_results = [[i for i in b if isinstance(i, (int, float))] for b in results]
for a, b in enumerate(filtered_results):
for i in b:
if i > highest:
highest = i
row = a
total = sum(b)
如果希望索引和子列表具有最高的单个值:
results = [[213, 124, 100],
[123.7, 444.6, 111, 12],
[],
[22, 11, 100, 2],
[-1000]]
def try_mx(x):
try:
return max((i for i in x[1] if isinstance(i, (int, float))))
except ValueError:
return float("-inf")
mx_ind = max(enumerate(results), key=try_mx)
print(mx_ind)
这将为您提供索引和子列表:
(1, [123.7, 444.6, 111, 12])
在您自己的代码中,每次如果i>highest
为True,则调用sum时,如果您找到了更高的值,而不是每次找到更高的值,则每个子列表只应调用sum一次
您可以再次使用max,以便仅当最高值大于当前最高值时才调用sum,使用python3,您可以为max提供默认值以捕获空列表:
highest, row, total = float("-inf"), 0, 0
for a, b in enumerate(results):
mx = max((i for i in b if isinstance(i, (int,float))), default=float("-inf"))
if mx > highest:
highest = mx
row = a
total = sum(b)
如果希望索引和子列表具有最高的单个值:
results = [[213, 124, 100],
[123.7, 444.6, 111, 12],
[],
[22, 11, 100, 2],
[-1000]]
def try_mx(x):
try:
return max((i for i in x[1] if isinstance(i, (int, float))))
except ValueError:
return float("-inf")
mx_ind = max(enumerate(results), key=try_mx)
print(mx_ind)
这将为您提供索引和子列表:
(1, [123.7, 444.6, 111, 12])
在您自己的代码中,每次如果i>highest
为True,则调用sum时,如果您找到了更高的值,而不是每次找到更高的值,则每个子列表只应调用sum一次
您可以再次使用max,以便仅当最高值大于当前最高值时才调用sum,使用python3,您可以为max提供默认值以捕获空列表:
highest, row, total = float("-inf"), 0, 0
for a, b in enumerate(results):
mx = max((i for i in b if isinstance(i, (int,float))), default=float("-inf"))
if mx > highest:
highest = mx
row = a
total = sum(b)
好的,total=sum(对于b中的i,如果不是i,str的话)不存在work@Zorgmorduk,这是因为它应该是
sum(i表示b中的i,如果不是instance(i,str))
,此时需要i
start@Zorgmorduk:我错了,打字错误;开始时有一个i
不见了。谢谢你们两位!是的,后一种方法非常有效。好的,total=sum(对于b中的i,如果不是isinstance(i,str))不起作用work@Zorgmorduk,这是因为它应该是sum(i表示b中的i,如果不是instance(i,str))
,此时需要i
start@Zorgmorduk:我错了,打字错误;开始时有一个i
不见了。谢谢你们两位!是的,后面的这个很好用。你用零初始化最高值,但是你的列表可以包含负值。如果所有的值都为负数,代码将错误地返回零。正确的方法通常是使用try/except,而不是str不会使某个值成为数字。为什么有任何值字符串?通常情况下,您希望程序在出现TypeError
的情况下中止,这样您就可以注意到问题并解决它。如何total=sum(b)
工作?即使你找到了一个更高的值,你也在不断地循环,每次你找到一个好的点@bipll时都会调用sum,谢谢!您用零初始化highest,但列表列表可以包含负值。如果所有的值都为负数,代码将错误地返回零。正确的方法通常是使用try/except,而不是str不会使某个值成为数字。为什么有任何值字符串?通常情况下,您希望程序在出现TypeError
的情况下中止,这样您就可以注意到问题并解决它。如何total=sum(b)
工作?即使你找到了一个更高的值,你也在不断地循环,每次你找到一个好的点@bipll时都会调用sum,谢谢!