Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中,如何检查数字列表是否介于2个值之间?_Python_List_Compare - Fatal编程技术网

在python中,如何检查数字列表是否介于2个值之间?

在python中,如何检查数字列表是否介于2个值之间?,python,list,compare,Python,List,Compare,我有一个数字列表,我需要对其进行排序,以了解该列表中是否有任何数字介于2个值之间(在我的示例中,介于1,50和2,50之间) 例: 我尝试了以下代码,但出现了一个错误:“TypeError:unorderable types:str()根据具体操作,可以使用for循环 列表中的值的: 如果“1,50”取决于您到底想做什么,您可以使用for循环 列表中的值的: 如果“1,50”第一件事是第一件事:请不要使用list作为变量,因为它是python提供的内置类型。使用list作为变量,您将覆盖它对内置

我有一个数字列表,我需要对其进行排序,以了解该列表中是否有任何数字介于2个值之间(在我的示例中,介于1,50和2,50之间)

例:


我尝试了以下代码,但出现了一个错误:“TypeError:unorderable types:str()根据具体操作,可以使用for循环

列表中的值的

如果“1,50”取决于您到底想做什么,您可以使用for循环

列表中的值的

如果“1,50”第一件事是第一件事:请不要使用list作为变量,因为它是python提供的内置类型。使用list作为变量,您将覆盖它对内置类型list的引用

第二:对于算术运算或表达式,建议将数字字符串转换为float或int

values = ['3,25', '3,15', '1,78', '2,10', '1,06', '1,58', '1,88', '1,19', '4,00', '2,45', '2,25', '3,00', '2,95', '2,45', '2,30', '1,52', '1,96', '6,50', '4,20', '1,27']
found = [i for i in map(lambda n: float(n.replace(',', '.')), values) if 1.50 < i < 2.50]

for i in found:
   print(i)

第一件事是:请不要使用list作为变量,因为它是python提供的内置类型。使用list作为变量,您将覆盖它对内置类型list的引用

第二:对于算术运算或表达式,建议将数字字符串转换为float或int

values = ['3,25', '3,15', '1,78', '2,10', '1,06', '1,58', '1,88', '1,19', '4,00', '2,45', '2,25', '3,00', '2,95', '2,45', '2,30', '1,52', '1,96', '6,50', '4,20', '1,27']
found = [i for i in map(lambda n: float(n.replace(',', '.')), values) if 1.50 < i < 2.50]

for i in found:
   print(i)
对于列表中的元素:
如果列表中元素的“1,50”

如果“1,50”,您可以使用filter获取列表,但首先应将其值更改为float,此外,不要使用list作为变量名。list是您需要的内置操作,不应被重写,可能会导致一些不良副作用:

list_ = ['3,25', '3,15', '1,78', '2,10', '1,06', '1,58', '1,88', '1,19', '4,00', '2,45', '2,25', '3,00', '2,95', '2,45', '2,30', '1,52', '1,96', '6,50', '4,20', '1,27']

list_number = [float(number.replace(",",".")) for number in list_]    
found = filter(lambda x: 1.50 <= x <= 2.50, list_number)

for value in found:
    print(value)

您可以使用filter获取列表,但首先应将其值更改为float,此外,不要将list用作变量名。list是您需要的内置操作,不应被重写,可能会导致一些不良的副作用:

list_ = ['3,25', '3,15', '1,78', '2,10', '1,06', '1,58', '1,88', '1,19', '4,00', '2,45', '2,25', '3,00', '2,95', '2,45', '2,30', '1,52', '1,96', '6,50', '4,20', '1,27']

list_number = [float(number.replace(",",".")) for number in list_]    
found = filter(lambda x: 1.50 <= x <= 2.50, list_number)

for value in found:
    print(value)

首先,列表中的所有字符串的格式都不正确。Python将在尝试将字符串解析为数字时引发异常:

ValueError:无法将字符串转换为浮点:“3,25”)

这是因为字符串中有一个逗号(“,”),而不是peroid(“”)。在将其转换为特定数据类型之前,需要将所有逗号替换为peroid。下面的示例

# Bad name, use something else as "list" is a built-in type    
list = ['3,25', '3,15', '1,78', '2,10', '1,06', '1,58', '1,88', '1,19', '4,00', '2,45', '2,25', '3,00', '2,95', '2,45', '2,30', '1,52', '1,96', '6,50', '4,20', '1,27']

# The numbers to extract from the list of strings
numbers = []

# For each number (as string), reformat the string and then cast it to a floating number 
for number_str in list:
   number = float(number_str.replace(',', '.'))
   numbers.append(number)
然后,您可以检查是否有minmax值:

for n in numbers:
   if 1.50 <= n <= 2.50:
      print("{} is between 1.50 and 2.50".format(n))
      break
对于n个数字:

首先,如果1.50,则列表中的所有字符串的格式都不正确。Python将在尝试将字符串解析为数字时引发异常:

ValueError:无法将字符串转换为浮点:“3,25”)

这是因为字符串中有一个逗号(“,”),而不是peroid(“”)。在将其转换为特定数据类型之前,需要将所有逗号替换为peroid。下面的示例

# Bad name, use something else as "list" is a built-in type    
list = ['3,25', '3,15', '1,78', '2,10', '1,06', '1,58', '1,88', '1,19', '4,00', '2,45', '2,25', '3,00', '2,95', '2,45', '2,30', '1,52', '1,96', '6,50', '4,20', '1,27']

# The numbers to extract from the list of strings
numbers = []

# For each number (as string), reformat the string and then cast it to a floating number 
for number_str in list:
   number = float(number_str.replace(',', '.'))
   numbers.append(number)
然后,您可以检查是否有minmax值:

for n in numbers:
   if 1.50 <= n <= 2.50:
      print("{} is between 1.50 and 2.50".format(n))
      break
对于n个数字:

如果1.50列表理解是一个选项:
found=[x代表我的_列表中的x如果1,5 Hi,是否需要检查区间[1.50,2.50]中是否有数字,或者区间中元素'1,50'和'2,50'之间是否有任何元素,然后将这些数字添加到名为“found”的变量中“列表理解是一个选项:
found=[x代表我的列表中的x如果1,5 Hi,是否需要检查间隔[1.50,2.50]中是否有数字,或者间隔中元素'1,50'和'2,50'之间是否有任何元素,然后将这些数字添加到名为“found”的变量中?”“要使用算术运算,值必须是数字(整数或浮点数)“。事实上,这不是真的。Python很乐意根据字符串的值逐字符比较字符串。例如
'1,50'@kopecs感谢+1的注意。对于算术运算,它必须是int或float。字符串可以在条件表达式中使用。Python使用其编码(ASCII)对字符串进行比较代码点例如:“/”<“0”evals True,“9”<:“evaluates True。然而,数字字符串比较工作得很好,因为python通过charI比较char。我不确定python是否需要ASCII(如果有的话,我假设它的UTF-8),但据我所知,0-9总是在一个很好的连续块*中,只要前导位数是多少(即9月10日之前)同样,我认为这实际上是相当可移植的——特别是它不像调用
float
is.*那样依赖于语言环境。除了ebcdiche的一些神秘方言之外,这里有一个简单的例子说明为什么字符串比较不能像预期的那样对数字字符串起作用:“9'<'10'evals False,'99.0'='99'evals False,'99.9'<'100.0'evals False,'99,9'<'100,0'evals False。字符串比较对于数字字符串并不总是理想的。如我所说,假设前导位数相同,它就可以工作——如果打破该前提条件,结果显然不会出现:)“要使用算术运算,值必须是数字(整数或浮点数)”。事实上,这不是真的。Python很乐意根据字符串的值逐字符比较字符串。例如
'1,50'@kopecs感谢+1的注意。对于算术运算,它必须是int或float。字符串可以在条件表达式中使用。Python使用其编码(ASCII)对字符串进行比较代码点例如:“/”<“0”evals True,“9”<:“evaluates True。然而,数字字符串比较工作得很好,因为python通过charI比较char。我不确定python是否需要ASCII(如果有的话,我假设它的UTF-8),但据我所知,0-9总是在一个很好的连续块*中,只要前导位数是多少(即十进制9月之前)是相同的,我认为这实际上是相当可移植的——特别是因为它不像调用
float
is.*那样依赖于语言环境。除了ebcdi的一些晦涩方言之外,这里有一个简单的例子说明为什么字符串比较不能像预期的那样对数字字符串起作用:“9”# Bad name, use something else as "list" is a built-in type list = ['3,25', '3,15', '1,78', '2,10', '1,06', '1,58', '1,88', '1,19', '4,00', '2,45', '2,25', '3,00', '2,95', '2,45', '2,30', '1,52', '1,96', '6,50', '4,20', '1,27'] # The numbers to extract from the list of strings numbers = [] # For each number (as string), reformat the string and then cast it to a floating number for number_str in list: number = float(number_str.replace(',', '.')) numbers.append(number)
for n in numbers:
   if 1.50 <= n <= 2.50:
      print("{} is between 1.50 and 2.50".format(n))
      break