简体   繁体   English

如何在python中将多个循环'for'设置为函数的参数以找到将句号分成n部分的所有可能性?

[英]How to set a number of loop 'for' as a parameter of a function in python to find all the possibility to cut a period in n parts?

Here is a ugly code to find all the possibilities to cut a period in 5 parts.这是一个丑陋的代码,用于查找将一个句点分成 5 个部分的所有可能性。 Is there a possibility to create a function which makes it look better, with the number of cut as a parameter?是否有可能创建一个使它看起来更好的函数,并将切割次数作为参数?

I am only abble to write each for loop:我只能编写每个for循环:

part_list = pd.DataFrame(columns=['period_array'])
for i in range(1, period_size):
    for j in range(1, period_size - i):
        for h in range(1, period_size - (i + j)):
            for g in range(1, period_size - (i + j + h)):
                part_list = part_list.append({'period_array':
                                                  np.array([[0, i],
                                                            [i, i + j],
                                                            [i + j, i + j + h],
                                                            [i + j + h, i + j + h + g],
                                                            [i + j + h + g, period_size]])},
                                             ignore_index=True)

Using function combinations from module itertools to generate all combinations of cutting points:使用模块 itertools中的函数combinations来生成切割点的所有组合:

from itertools import combinations, chain, pairwise

def all_cuts(seq, n):
    for comb in combinations(range(1,len(seq)), n-1):
        yield tuple(seq[i:j] for i,j in pairwise(chain((0,), comb, (len(seq),))))

print( list(all_cuts('Hello, World!', 3)) )
# [('H', 'e', 'llo, World!'), ('H', 'el', 'lo, World!'),
#  ('H', 'ell', 'o, World!'), ('H', 'ello', ', World!'),
#  ('H', 'ello,', ' World!'), ...
#                        ..., ('Hello, Wor', 'l', 'd!'),
#  ('Hello, Wor', 'ld', '!'), ('Hello, Worl', 'd', '!')]

I modified the code to have a shorter running time with a dataframe and the apply method (the 'for loop' isn't efficient with long sequences).我修改了代码,使数据帧和应用方法的运行时间更短(“for 循环”对于长序列来说效率不高)。

For 3 period in a list of 52, I reduce the time from 14.6s to 0.2s.对于 52 个列表中的 3 个周期,我将时间从 14.6 秒减少到 0.2 秒。

Here is my final code: Many thanks @Stef :)这是我的最终代码:非常感谢@Stef :)

    COMBINATION_VECTOR = 'combination vector'
    PERIOD_ARRAY = 'period array'
    
    def get_partition_table(period_resolution: int, number_of_changes: int):
        """
        This function creates a dataframe of all the possible combination of date of change over a year
        :param period_resolution: resolution (month : 12, week: 52 or days: 365)
        :param number_of_changes
        :return: dataframe of each period table
        """
    
        scale_factor = 365 // period_resolution
    
        def from_combinations_to_period_array(comb):
            """
            This returns the period array of the specific combination : [[0, d1], [d1, d2] ... [dn, 365]]
            :param comb: combinations of dates to create the period array
            :return: period array
            """
            return np.array([[i, j] for i, j in pairwise(it.chain((0,), comb, (period_resolution,)))])
    
        part_list = pd.DataFrame(
            {COMBINATION_VECTOR: list(it.combinations(range(1, period_resolution), number_of_changes))})
    
        part_list[PERIOD_ARRAY] = part_list.apply(
            lambda x: from_combinations_to_period_array(x[COMBINATION_VECTOR]), axis=1)
        
        part_list.drop(COMBINATION_VECTOR, axis=1)
    
        return part_list * scale_factor

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python和数论:我们如何为q(n)(n划分为不同部分的分区数)创建生成函数? - Python and Number Theory: How can we create the generating function for q(n) (the number of partitions of n into distinct parts)? 如何使用Python并遵循一组最小和最大要求在N个不均匀部分中划分数值? - How to divide a number value in N unevenly parts using Python & following a set of min- & max requirement? 是否有任何 python 模块将数字 N 分成 3 个部分,使得这些部分的总和等于 N? 或者怎么做? - Is there any python module to divide a number N into 3 parts such that sum of that parts equal to N? or how to do that? 如何使用 python 重复部分剪辑 n 次 - How to repeat parts of clips n number of time using python 如何使用Python中的循环使用find函数过滤excel中的所有行? - How to filter all rows in excel with find function using loop in Python? python3 计算 PI 的 N 位数 python 切长数 - python3 calculate N digits of PI python cut the long number 寻找将随机数插入字符串的所有可能性? (Python) - Find every possibility to insert random number into a string? (python) Python:如何将数据帧设置为python中函数的参数? - Python : How to set dataframe as parameter to a function in python? 如何将列表分成n个相等的部分,python - How to divide a list into n equal parts, python 如何在 Python 中将字符串分成“n”部分 - How to divide a string into 'n' parts in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM