删除范围存储桶中的数据

时间:2012-12-02 00:22:47

标签: python

我有二维数据存储在元组的排序列表中,如下所示:

data = [(0.1,100), (0.13,300), (0.2,10)...

每个元组中的第一个值(X值)仅对元组列表出现一次。换句话说,0.1等只能有一个值

然后我有一个已排序的存储桶列表。存储桶定义为包含范围和ID的元组,如下所示:

buckets = [((0,0.14), 2), ((0.135,0.19), 1), ((0.19,0.21), 2), ((0.19,0.24), 3)...

范围相对于X轴。因此,id 2上面有两个桶,id 1和3分别只有一个桶。 id 2的第一个桶的范围是0到0.14。请注意,水桶可以重叠。

所以,我需要一种算法将数据丢弃到桶中,然后将分数相加。对于上面的数据,结果将是:

1:0
2:410
3:10

注意每个数据如何被与ID 2相关联的存储桶捕获,因此得分100+300+10=410

我如何编写算法来执行此操作?

3 个答案:

答案 0 :(得分:1)

试试这段代码:

data = [(0.1,100), (0.13,300), (0.2,10)]
buckets = [((0,0.14), 2), ((0.135,0.19), 1), ((0.19,0.21), 2), ((0.19,0.24), 3)]

def foo(tpl): ## determine the buckets a data-tuple is enclosed by list of IDs
    x, s = tpl
    lst = []
    for bucket in buckets:
        rnge, iid = bucket
        if x>rnge[0] and x<rnge[1]: lst.append(iid)
    return lst

data = [[dt, foo(dt)] for dt in data]

scores_dict = {}
for tpl in data:
    score = tpl[0][1]
    for iid in tpl[1]:
        if iid in scores_dict: scores_dict[iid]+=score
        else:                  scores_dict[iid] =score

for key in scores_dict:
    print key,":",scores_dict[key]

此代码段会产生:

2 : 410
3 : 10

如果未打印任何存储桶ID,则该存储桶中没有X值,或者它总和为零。

答案 1 :(得分:1)

将每个存储桶定义(标签范围)转换为可调用的 - 在给定数据元组的情况下 - 将增加存储桶总数。存储桶值存储在简单的字典中。如果你想提供一个更简单的api,你可以轻松地将这个概念包装在一个类中。

def partition(buckets, bucket_definition):
    """Build a callable that increments the appropriate buckets with a value"""

    lower, upper = bucket_definition[0]
    key = bucket_definition[1]

    def _partition(data):
        x, y = data
        # Set a default value for this key
        buckets.setdefault(key, 0)

        if lower <= x <= upper:
            buckets[key] += y

    return _partition


bucket_definitions = [
    ((0, 0.14), 2),
    ((0.135, 0.19), 1),
    ((0.19, 0.21), 2),
    ((0.19, 0.24), 3)
]

data = [(0.1, 100), (0.13, 300), (0.2, 10)]

# Holder for bucket labels and values
buckets = {}

# For each bucket definition (range, label) build a callable
partitioners = [partition(buckets, definition) for definition in bucket_definitions]

# Map each callable to each data tuple provided
for partitioner in partitioners:
    map(partitioner, data)

print(buckets)

答案 2 :(得分:1)

这将从您的测试数据中生成所需的输出:

data = [(0.1,100), (0.13,300), (0.2,10)]
buckets = [((0,0.14), 2), ((0.135,0.19), 1), ((0.19,0.21), 2), ((0.19,0.24), 3)]

totals = dict()

for bucket in buckets:
    bucket_id = bucket[1]
    if bucket_id not in totals:
        totals[bucket_id] = 0
    for data_point in data:
        if data_point[0] >= bucket[0][0] and data_point[0] <= bucket[0][1]:
            totals[bucket_id] += data_point[1]

for key in sorted(totals):
    print("{}: {}".format(key, totals[key]))