[英]Python3 find groups of strictly increasing “ float values” within a list
I cannot find a solution to find groups of strictly increasing "float values" within a list.我找不到在列表中找到严格增加的“浮点值”组的解决方案。 All examples I find online are all group by "integers" type values.
我在网上找到的所有示例都是按“整数”类型值分组的。 A simple example of what I am looking for is code that picks up the float value sequence as well...see my list below for clarity:
我正在寻找的一个简单示例是获取浮点值序列的代码......为了清楚起见,请参阅下面的列表:
l = [8, 9, 10, -1.2, -1.1,-1.0, -10, -11, -12, 7, 8, 1.2, 2.3, 3.4] l = [8, 9, 10, -1.2, -1.1,-1.0, -10, -11, -12, 7, 8, 1.2, 2.3, 3.4]
the code below returns only the integer numbers and simply ignores the floats.下面的代码仅返回 integer 数字并忽略浮点数。 If I would use the "round" function to change the list into integers, it would defeat the whole purpose of the effort...
如果我使用“圆形” function 将列表更改为整数,它将破坏整个努力的目的......
What I would like to achieve is an output of strictly increasing "float values" from the list above returning the output values of: [[8, 9, 10],[-1.2,-1.1,-1.0], [7, 8], [1.2, 2.3, 3.4]]我想要实现的是从上面的列表中严格增加“浮点值”的 output 返回 output 值: [[8, 9, 10],[-1.2,-1.1,-1.0], [7, 8 ], [1.2, 2.3, 3.4]]
Code to explain the problem best:最好地解释问题的代码:
def groupSequence(x):
it = iter(x)
prev, res = next(it), []
while prev is not None:
start = next(it, None)
if prev + 1 == start:
res.append(prev)
elif res:
yield list(res + [prev])
res = []
prev = start
l = [8, 9, 10, -1.2, -1.1 ,-1.0, -10, -11, -12, 7, 8, 1.2, 2.3, 3.4]
print(list(groupSequence(l)))
#current output: [[8, 9, 10], [7, 8]]
#当前 output:
[[8, 9, 10], [7, 8]]
#desired output [[8, 9, 10],[-1.2,-1.1,-1.0], [7, 8], [1.2, 2.3, 3.4]]
#desired output
[[8, 9, 10],[-1.2,-1.1,-1.0], [7, 8], [1.2, 2.3, 3.4]]
Any help would be much appreciated.任何帮助将非常感激。
The problem is in this line:问题出在这一行:
if prev + 1 == start:
The floats in your sequence doesn't increase by one: -1.2 + 1 <> -1.1
序列中的浮点数不会增加一:
-1.2 + 1 <> -1.1
if start and start > prev:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.