使用Python删除重叠的元组值

时间:2017-11-22 01:58:59

标签: python algorithm

我有一个包含N个元组的元组列表(我们将其命名为yz_list),其中包含起始值和结束值,如:(start,end),由下面的示例表示:

bool error = false;    
for (int i = 0; i < c.length(); i ++)
    {
       if (!isdigit(c[i]))
       {
          error = true;
          break;
       }
    }

我想删除所有与之前保存的元组间隔重叠的值。在上面显示的序列上代表这种情况的输出是:

yz_list = [(0, 6), (1, 7), (2, 8), (3, 9), (4, 10), (5, 11), (6, 12), (18, 24)]

如何使用Python实现此结果?

编辑#1

以下代码是我生成此元组的代码:

 result = [(0,6), (6,12), (18,24)]

当我使用TDD开发时,我正在逐步进行开发,现在我正在考虑如何实现删除重叠元组。我真的不知道将它们用作集合是否是个好主意,并查看重叠的项目。

用于生成结果列表的伪代码是:

for i, a in enumerate(seq):
    if seq[i:i+multiplier] == "x"*multiplier:
        to_replace.append((i, i+multiplier))

for i, j in enumerate(to_replace):
    print(i,j)
    if i == 0:
        def_to_replace.append(j)
    else:
        ind = def_to_replace[i-1]
        print(j[0]+1, "\n", ind)
        if j[0]+1 not in range(ind[0], ind[1]):
            def_to_replace.append(j)
    # print(i, j)
print(def_to_replace)
for item in def_to_replace:
    frag = replacer(frame_calc(seq[:item[0]]), rep0, rep1, rep2)
    for k, v in enumerate(seq_dup[item[0]:item[1]]):
        seq_dup[int(item[0]) + int(k)] = list(frag)[k]

return "".join(seq_dup)

3 个答案:

答案 0 :(得分:1)

这可能有用。没有花哨的东西,只需手动处理每个元组,看看是否有值在保存的元组设置范围的范围内:

yz_list = [(0, 6), (1, 7), (2, 8), (3, 9), (4, 10), (5, 11), (6, 12), (18, 24)]

result = [yz_list[0]]

bounds = yz_list[0][0], yz_list[0][1]

for tup in yz_list[1:]:
    if tup[0] in range(bounds[0], bounds[1]) or tup[1] in range(bounds[0], bounds[1]):
        pass
    else:
        result.append(tup)

print result # [(0, 6), (6, 12), (18, 24)]

答案 1 :(得分:0)

这是一个使用高效二进制搜索计算重叠的类,以及显示其用于解决问题的代码。用python3运行。

import bisect
import sys

class Overlap():

    def __init__(self):
        self._intervals = []

    def intervals(self):
        return self._intervals

    def put(self, interval):
        istart, iend = interval
        # Ignoring intervals that start after the window.                                       
        i = bisect.bisect_right(self._intervals, (iend, sys.maxsize))

        # Look at remaining intervals to find overlap.                                          
        for start, end in self._intervals[:i]:
            if end > istart:
                return False
        bisect.insort(self._intervals, interval)
        return True


yz_list = [(0, 6), (1, 7), (2, 8), (3, 9), (4, 10), (5, 11), (6, 12), (18, 24)]

ov = Overlap()
for i in yz_list:
    ov.put(i)

print('Original:', yz_list)
print('Result:', ov.intervals())

输出:

Original: [(0, 6), (1, 7), (2, 8), (3, 9), (4, 10), (5, 11), (6, 12), (18, 24)]                 
Result: [(0, 6), (6, 12), (18, 24)] 

答案 2 :(得分:0)

yz_list = [(0, 6), (1, 7), (2, 8), (3, 9), (4, 10), (5, 11), (6, 12), (18, 24)]

result = []
for start, stop in yz_list:
    for low, high in result:
        if (low < start < high) or (low < stop < high):
            break
    else:
        result.append((start, stop))

这提供了所需的输出,并且很容易看出它是如何工作的。 else条款基本上只是意味着,如果我们没有break退出循环,那就运行这个&#34;。