使用装饰器与迭代来设置值?

时间:2013-05-30 22:21:07

标签: python

所以我必须遍历一个对象列表,使用它们的一些值来进行计算,然后为它们分配新的值。

因为列表中的许多项目将被赋予相同的新值,所以我使用字典来保存需要相同值的项目列表。例如:

item_dict = {}

for item in list:
    value = item.value
    if value not in item_dict:
        item_dict[value] = [item]
    else:
        item_dict[value].append(item)

# do some calculations base on values

new_data # some dictionary created by computation
# new data is stored new_data[value] = new_value

for value, new_value in new_data.items():
    items = item_dict[value]
    for item in items:
        item.value = new_value

我考虑使用装饰器删除items循环中的for项,因为该列表的所有new_value都是相同的。例如:

def dec(item):
    def wrap(value):
        item.value = value
    return wrap

def rec(item, func):
    def wrap(value):
        item.value = value
        func(value)
    return wrap

item_dict = {}

for item in list:
    value = item.value
    if value not in item_dict:
        item_dict[value] = dec(item)
    else:
        item_dict[value] = rec(item, item_dict[value])

# do some calculations base on values

new_data # some dictionary created by computation
# new data is stored new_data[value] = new_value

for value, new_value in new_data.items():
    items = item_dict[value]
    items(new_value)

装饰者时尚会更高效吗?它会对内存产生多大的影响?有没有更好的方法来做到这一点?

2 个答案:

答案 0 :(得分:1)

defaultdict在这里运作良好:

from collections import defaultdict

item_dict = defaultdict(list)

for item in value_list:
    item_dict[item.value].append(item)

# do some calculations base on values

new_data # some dictionary created by computation
# new data is stored new_data[value] = new_value

for value, new_value in new_data.items():
    for item in item_dict[value]:
        item.value = new_value

我很难想到装饰器版本可能更好的方式 - 首先,你必须担心递归限制。

答案 1 :(得分:0)

get方法适用于第一种情况。

item_dict = {}

for item in list:
    item_dict[item.value] = item_dict.get(item.value, []) + [item]

使这项工作的关键是使用列表添加而不是追加,因为append返回None。