简体   繁体   English

我收到TypeError:“ str”和“ int”的实例之间不支持“ <”

[英]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中, strint实例之间的顺序是未定义的,并引发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.

相关问题 如何解决 TypeError: &#39;int&#39; 和 &#39;str&#39; 实例之间不支持的 &#39;&lt;&#39;? - How do I resolve TypeError: '<' not supported between instances of 'int' and 'str'? 类型错误:“str”和“int”的实例之间不支持“&gt;”,但我将字符串设为 Interger - TypeError: '>' not supported between instances of 'str' and 'int' but I made the string an Interger 我有这个错误 TypeError: &#39;&lt;&#39; not supported between &#39;str&#39; and &#39;int&#39; - i am having this error TypeError: '<' not supported between instances of 'str' and 'int' “TypeError: &#39;&gt;&#39; 在 &#39;int&#39; 和 &#39;str&#39; 的实例之间不受支持”最大值 - "TypeError: '>' not supported between instances of 'int' and 'str'" in max Python-&#39;str&#39;和&#39;int&#39;的实例之间不支持&#39;TypeError:&#39;&lt;=&#39; - Python - 'TypeError: '<=' not supported between instances of 'str' and 'int'' Paramiko TypeError:'int'和'str'的实例之间不支持'&lt;' - Paramiko TypeError: '<' not supported between instances of 'int' and 'str' TypeError: '&lt;=' 在 'str' 和 'int' 的实例之间不支持 - TypeError: '<=' not supported between instances of 'str' and 'int' TypeError: '>' 在 'str' 和 'int' 的实例之间不支持 - TypeError: '>' not supported between instances of 'str' and 'int' NLTK TypeError:&#39;str&#39;和&#39;int&#39;的实例之间不支持&#39;&lt;&#39; - NLTK TypeError: '<' not supported between instances of 'str' and 'int' Flask TypeError:“str”和“int”的实例之间不支持“&lt;” - Flask TypeError: '<' not supported between instances of 'str' and 'int'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM