生成具有唯一限制的每个排列

2024-09-29 17:11:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一个测试脚本,它将根据多个列表生成所有可能的值排列。但是,对于哪些值可以与其他值匹配存在一些限制。到目前为止,我得出了以下结论:

fieldTypes = ['Text', 'Float', 'Double', 'Short', 'Long', 'Date']
domainTypes = ['Coded', 'Range']
rangeSplitPolicies = ['Default', 'Duplicate', 'Geometry_Ratio']
rangeMergePolicies = ['Default', 'Area_Weighted', 'Sum_Values']
codedSplitPolicies = ['Default', 'Duplicate']
codedMergePolicies = ['Default']

for fieldType in fieldTypes:
    for domainType in domainTypes:
        # Skip incompatible domainType-fieldType permutations
        if domainType == 'Coded' and fieldType == 'Date' \
        or domainType == 'Range' and fieldType == 'Text':
           break

        # Range domain-type handling
        if domainType == 'Range':
            for splitPolicy in rangeSplitPolicies:
                # Date fields require special handling and only support the 
                # the default keyword for their split and merge policies
                if fieldType == 'Date':
                    permutation = '{0}-{1}'.format(
                        fieldType,
                        domainType,
                        'Default',
                        'Default')
                else:
                    for mergePolicy in rangeMergePolicies:
                        permutation = '{0}-{1}-{2}-{3}'.format(
                            fieldType,
                            domainType,
                            splitPolicy,
                            mergePolicy)

        # Coded-value domain-type handling
        else:
            for splitPolicy in codedSplitPolicies:
                for mergePolicy in codedMergePolicies:
                    permutation = '{0}-{1}-{2}-{3}'.format(
                        fieldType,
                        domainType,
                        splitPolicy,
                        mergePolicy)

        # Do more stuff...

显然,这不是很优雅。如果可能的话,我想使用生成器,因为我只需要获取每个置换一次,但我真的不确定最好的方法是什么来组织这个,获取字段和域类型的每个可能置换,以及每个域类型的适当拆分和合并策略,同时强制执行以下细微限制:

  • 日期字段不支持基于编码值的域
  • 文本字段不支持基于范围的域
  • 日期字段仅支持其拆分和合并策略的“Default”关键字。你知道吗

我现在只是把排列作为一个字符串,但它可以作为元组、列表或任何易于解析的东西返回。你知道吗

提前感谢您的帮助。你知道吗


Tags: andinformatdefaultfordateifrange
1条回答
网友
1楼 · 发布于 2024-09-29 17:11:05

听起来你不是说排列,而是笛卡尔积。你知道吗

考虑执行以下操作:

def gen(domain_types, field_types):
    for d, f in itertools.product(domain_types, field_types):
        if is_okay(d, f):
            yield d, f

我知道你有更多的领域要担心,但这个例子应该足够了。你知道吗

相关问题 更多 >

    热门问题