需要帮助思考在Python中的整数除法中拆分列表

时间:2014-03-01 20:23:02

标签: python list numpy insert split

我目前有一个数据集作为列表列表,我想在整数除法中拆分,如果有重叠则插入新数据。例如:

编辑:数据集始终按升序排序。

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]时,因为这是上一个列表元素中不存在的全新信息。

非常感谢任何帮助。

3 个答案:

答案 0 :(得分: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]]

答案 1 :(得分:0)

我通过在列表[7.025337,7.871763],[12.,12.2]中添加最后一个差距来扩展问题,这使我的代码有点尴尬但没有导入模块

list = [[1.565888, 2.073744], [2.073744, 2.962492], [2.962492, 4.52838], [4.52838, 5.417127], [5.417127, 6.390517], [7.025337, 7.871763],[12.,12.2]]

newlist= []
for e in list:
    if list.index(e)>0:
        if z+1<= int(e[0]):
            while z < int(e[0]):
                newlist.append([float(z),float(z+1)])
                z += 1


    if int(e[0])==int(e[1]):
        newlist.append(e)
    elif int(e[0])<int(e[1]):
        y = int(e[0]+1)
        newlist.append( [e[0],float(y)] )
        while y < int(e[1]):
            newlist.append([float(y),float(y+1)])
            y +=1

        newlist.append([float(y),e[1]])
    z = int(e[1])
print (newlist)

给出:

[[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], 
[6.0, 7.0], [7.025337, 7.871763], [7.0, 8.0], [8.0, 9.0], [9.0, 10.0], [10.0, 11.0], 
[11.0, 12.0], [12.0, 12.2]]

答案 2 :(得分:0)

def splitter(iterable):
    from collections import deque
    q = deque(maxlen=2)
    flatlist = (i for j in iterable for i in j)
    it = iter(flatlist)
    q.append(next(it))

    for val in it:
        if val == q[-1]: continue
        while (int(q[-1]) + 1 < val):
            q.append(int(q[-1])+1.0)
            yield [q[0], q[1]]
        q.append(val)
        yield [q[0], q[1]]

list_set = [[1.565888, 2.073744], [2.073744, 2.962492], [2.962492, 4.52838], [4.52838, 5.417127], [5.417127, 6.390517], [7.025337, 7.871763]]
print list(splitter(list_set))
#[[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], [6.390517, 7.0], [7.0, 7.025337], [7.025337, 7.871763]]