Python 3.x 在Python3中查找整数和字符串列表中最小数的索引

Python 3.x 在Python3中查找整数和字符串列表中最小数的索引,python-3.x,Python 3.x,我以前在Python2.7中有代码,我可以在整数和字符串列表中找到最低数字的索引。我之前用下面的例子做过这件事: list = ["NA",2,3,1] min_num_position = list.index(min(list)) return min_num_position >>>3 我已经升级到Python 3,上面的代码会抛出一个类型错误,因为我混合了字符串和整数: TypeError: '<' not supported between instan

我以前在Python2.7中有代码,我可以在整数和字符串列表中找到最低数字的索引。我之前用下面的例子做过这件事:

list = ["NA",2,3,1]

min_num_position = list.index(min(list))

return min_num_position

>>>3
我已经升级到Python 3,上面的代码会抛出一个类型错误,因为我混合了字符串和整数:

TypeError: '<' not supported between instances of 'int' and 'str'

TypeError:“您可以构建一个不带字符串的子列表,然后调用
min

min_num = lst.index(min([x for x in lst if not isinstance(x, str)]))
当然,如果您需要字符串仍然存在时的“原始”索引,您可以将其替换为一个巨大的数字(如果您知道其他数字的上限),这样它们就永远不会是“min”值:

min_num = lst.index(min([x if not isinstance(x, str) else 99999 for x in lst]))