[英]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.