我目前有一个数据集作为列表的列表,我想按整数除法拆分它,如果有重叠则插入新数据。例如:
编辑:数据集总是升序排列。
data = [[1.565888, 2.073744], [2.073744, 2.962492], [2.962492, 4.52838], [4.52838, 5.417127], [5.417127, 6.390517], [7.025337, 7.871763]]
fix_list(data)
#[[1.565888, 2.0], [2.0, 2.073744], [2.073744, 2.962492], [2.962492, 3.0], [3.0. 4.0], [4.0, 4.52838], [4.52838, 5.0], [5.0, 5.417127], [5.417127, 6.0], [6.0, 6.390517], [7.025337, 7.871763]]
然而,当我仔细思考如何解释每种情况时,我感到很茫然,特别是在插入 [3.0, 4.0] 时,因为这是全新的信息,在之前的列表元素中不存在。
我们将一如既往地感谢您的帮助。
最佳答案
更新
这看起来不那么干净,但现在所有整数对都被输出了,即使它们不在原始数据中。另一方面,现在它是用递归生成器实现的,我认为这有点巧妙:)
这可能会如您所愿。 int_split
是一个生成器,它扫描输入列表,如果对具有不同的整数值,则生成剪裁到整数边界的对。该算法单独保留 [3.0, 4.0]
之类的项目,因为它们已经在整数边界上。
import math
data = [[1.565888, 2.073744], [2.073744, 2.962492], [2.962492, 4.52838], [4.52838, 5.417127], [5.417127, 6.390517], [7.025337, 7.871763], [11.1, 12.1]]
def int_split(data):
last_b = data[0][1]
for item in data:
a, b = item
# First, make sure we haven't missed anything from the last loop
if math.floor(a) - math.ceil(last_b) > 1.0:
for x in int_split([[last_b, a]]):
yield x
# Did we cross an integer boundary, i.e.
# Is ceil(b) - floor(a) > 1.0?
if math.ceil(b) - math.floor(a) > 1.0:
# Yes, so split, making sure to include all integers between a and b
# Find any integers in the range (and convert to floats)
ints = [float(x) for x in range(int(math.ceil(a)), int(b))]
for c in ints:
# output those values
yield [a, c]
a = c
else:
yield [a, math.floor(b)]
yield [math.floor(b), b]
else:
yield item
# remember where we are
last_b = b
这个输出:
[[1.565888, 2.0], [2.0, 2.073744], [2.073744, 2.962492], [2.962492, 3.0],
[3.0, 4.0], [4.0, 4.52838], [4.52838, 5.0], [5.0, 5.417127], [5.417127, 6.0],
[6.0, 6.390517], [7.025337, 7.871763], [7.871763, 8.0], [8.0, 9.0],
[9.0, 10.0], [10.0, 11.0], [11.0, 11.1], [11.1, 12.0], [12.0, 12.1]]
关于python - 需要帮助思考在 Python 中以整数除法拆分列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22119877/