Python优雅的方式来映射字符串结构



假设我事先知道字符串

"key1:key2[]:key3[]:key4"应映射到"newKey1[]:newKey2[]:newKey3"

然后给出"key1:key2[2]:key3[3]:key4"

我的方法应返回"newKey1[2]:newKey2[3]:newKey3"

(正方形支架内的数字顺序应像上面的示例一样留下(

(

我的解决方案看起来像这样:

predefined_mapping = {"key1:key2[]:key3[]:key4": "newKey1[]:newKey2[]:newKey3"}

def transform(parent_key, parent_key_with_index):
    indexes_in_parent_key = re.findall(r'[(.*?)]', parent_key_with_index)
    target_list = predefined_mapping[parent_key].split(":")
    t = []
    i = 0
    for elem in target_list:
        try:
            sub_result = re.subn(r'[(.*?)]', '[{}]'.format(indexes_in_parent_key[i]), elem)
            if sub_result[1] > 0:
                i += 1
            new_elem = sub_result[0]
        except IndexError as e:
            new_elem = elem
        t.append(new_elem)
    print ":".join(t)

transform("key1:key2[]:key3[]:key4", "key1:key2[2]:key3[3]:key4")

newKey1[2]:newKey2[3]:newKey3打印为结果。

有人可以提出一个更好,优雅的解决方案(尤其是在正则围绕正则使用(吗?

谢谢!

您可以通过简单地将映射的结构拆分[]上,然后从实际数据中插入索引,然后将所有内容都加入:

import itertools
# split the map immediately on [] so that you don't have to split each time on transform
predefined_mapping = {"key1:key2[]:key3[]:key4": "newKey1[]:newKey2[]:newKey3".split("[]")}
def transform(key, source):
    mapping = predefined_mapping.get(key, None)
    if not mapping:  # no mapping for this key found, return unaltered
        return source
    indexes = re.findall(r'[.*?]', source)  # get individual indexes
    return "".join(i for e in itertools.izip_longest(mapping, indexes) for i in e if i)
print(transform("key1:key2[]:key3[]:key4", "key1:key2[2]:key3[3]:key4"))
# newKey1[2]:newKey2[3]:newKey3

注意:在Python上3使用itertools.zip_longest()

我仍然认为您对此进行了过度工程,并且对于整个问题来说可能存在更优雅且容易出错的方法。我建议退后一步,看大图,而不是仅仅因为它正在解决即时需求,而不是锤击这个特殊的解决方案。

相关内容

最新更新