[英]I got a TypeError: '<' not supported between instances of 'str' and 'int'
In python while printing minimum value in a list using min()
function gives me an error, when I try it in spyder Ide. 在python中,使用
min()
函数在列表中打印最小值时,在spyder Ide中尝试时会给我一个错误。 But when I run the same code in pycharm it works well. 但是,当我在pycharm中运行相同的代码时,效果很好。 What should I do to make it print on spyder Ide?
我应该怎么做才能在spyder Ide上打印?
This is my code 这是我的代码
lst = [101,754,'abcd','xyz','m']
Printing("Minimum value in List:", min(lst))
This gives an error: 这给出了一个错误:
TypeError: '<' not supported between instances of 'str' and 'int'
Most probably your spyder IDE defaults to Python 3 in your system, and your PyCharm project to Python 2. 您的系统中的spyder IDE最可能默认为Python 3,而PyCharm项目则默认为Python 2。
In Python 3, ordering between str
and int
instances is undefined and throws an Exception. 在Python 3中,
str
和int
实例之间的顺序是未定义的,并引发Exception。 In Python 2, IIRC it returns True or False based on the addresses ( id()
) of the two objects. 在Python 2中,IIRC根据两个对象的地址(
id()
)返回True或False。
Since some elements of the list are strings it can't find the minimum number in the list, since the min() function tries comparing the numbers with the string too. 由于列表中的某些元素是字符串,因此无法在列表中找到最小值,因为min()函数也会尝试将数字与字符串进行比较。
Try this: 尝试这个:
List = [101,754,'abcd','xyz','m']
numList = list(filter(lambda x: type(x)!=str, List)) # Creates a new list with only numbers
print("Minimum value in List:", min(numList))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.