扭转(或简化)笛卡儿积?

时间:2014-12-31 18:18:09

标签: python-2.7 cartesian-product radix-tree

为了使事情变得更容易但也更复杂,我试图实现" 组合/简明标签"的概念。进一步扩展到多个基本标签形式。

在这种情况下,标签由(一个或多个)"子标签"组成,由分号分隔:

food:fruit:apple:sour/sweet

drink:coffee/tea:hot/cold

wall/bike:painted:red/blue

斜杠表示"子标签"互换性。 因此,口译员将其翻译成:

food:fruit:apple:sour
food:fruit:apple:sweet

drink:coffee:hot
drink:coffee:cold
drink:tea:hot
drink:tea:cold

wall:painted:red
wall:painted:blue
bike:painted:red
bike:painted:blue

使用的代码(不完美,但有效):

import itertools

def slash_split_tag(tag):
    if not '/' in tag:
        return tag
    subtags = tag.split(':')
    pattern, v_pattern = (), ()
    for subtag in subtags:
        if '/' in subtag:
            pattern += (None,)
            v_pattern += (tuple(subtag.split('/')),)
        else:
            pattern += (subtag,)
    def merge_pattern_and_product(pattern, product):
        ret = list(pattern)
        for e in product:
            ret[ret.index(None)] = e
        return ret
    CartesianProduct = tuple(itertools.product(*v_pattern)) # http://stackoverflow.com/a/170248
    return [ ':'.join(merge_pattern_and_product(pattern, product)) for product in CartesianProduct ]

#===============================================================================
# T E S T
#===============================================================================

for tag in slash_split_tag('drink:coffee/tea:hot/cold'):
    print tag
print
for tag in slash_split_tag('A1/A2:B1/B2/B3:C1/C2:D1/D2/D3/D4/EE'):
    print tag

问题:我怎样才能恢复这个过程?出于可读性原因,我需要这个。

1 个答案:

答案 0 :(得分:1)

这是一个简单的第一次尝试这样的功能:

def compress_list(alist):
    """Compress a list of colon-separated strings into a more compact
    representation.
    """
    components = [ss.split(':') for ss in alist]

    # Check that every string in the supplied list has the same number of tags
    tag_counts = [len(cc) for cc in components]
    if len(set(tag_counts)) != 1:
        raise ValueError("Not all of the strings have the same number of tags")

    # For each component, gather a list of all the applicable tags. The set
    # at index k of tag_possibilities is all the possibilities for the
    # kth tag
    tag_possibilities = list()
    for tag_idx in range(tag_counts[0]):
        tag_possibilities.append(set(cc[tag_idx] for cc in components))

    # Now take the list of tags, and turn them into slash-separated strings
    tag_possibilities_strs = ['/'.join(tt) for tt in tag_possibilities]

    # Finally, stitch this together with colons
    return ':'.join(tag_possibilities_strs)

希望这些评论足以解释它的工作原理。但是有几点需要注意:

  • 如果它在标签列表中找到它们,它不会做任何明智的事情,例如转义反斜杠。

  • 这不能识别是否存在更微妙的划分,或者是否有不完整的标记列表。考虑这个例子:

    fish:cheese:red
    chips:cheese:red
    fish:chalk:red
    

    它不会意识到只有cheese同时包含fishchips,而是将其折叠为fish/chips:cheese/chalk:red

  • 完成字符串中标记的顺序是随机的(或者至少,我认为它与给定列表中字符串的顺序没有任何关系)。如果这很重要,您可以在使用斜杠加入之前对tt进行排序。

使用问题中给出的三个列表进行测试似乎有效,尽管如我所说,顺序可能与初始字符串不同:

food:fruit:apple:sweet/sour
drink:tea/coffee:hot/cold
wall/bike:painted:blue/red