python - 查找数组 Python 的最小值
问题描述
我有一个包含一些数值的数组和一个包含一些字符串的数组。就像这些数组一样:
t_nextevent = ["wake", "wake", "tired", "wake"]
t_nexttime = [8, 16, 7, 11]
t_nexttime
如果t_nextevent
等于 ,我想找到数组的最小值wake
。为此,我编写了这段代码。
t_next_event = 1000.0
t_index = 0
for i in range(num_i):
if (t_nextevent[i] != "tired" and t_nexttime[i] < t_next_event):
t_next_event = t_nextevent[i]
t_index = i
但是,我收到此错误。
TypeError: '<' not supported between instances of 'numpy.ndarray' and 'str'
任何人都可以帮我找到解决方案吗?最好的,
解决方案
试试这个代码:
min_val = min([int(t_nexttime[i]) for i in range(len(t_nexttime)) if t_nextevent[i] == 'wake'])
print(min_val)
它返回 中的值列表中的最小值t_nexttime
,其对应的t_nextevent
值为wake
。
这打印
8