英文:
How to make a comprehensive composition table efficiently
问题
I am trying to make a comprehensive composition table for composite development (in the chemistry field) based on the specific rules.
The rules are as follows:
- The numbers in a table represent the weight percentage of each raw material in the composite.
- There are three raw materials (A, B, C), and I can use up to two materials to make a composite.
- Each number in a table must be a multiple of x, and the sum of each row must be less than or equal to y.
- I have to write every possible combination that meets the rules above.
For example, if x=1 and y=2, I can create a table as shown below:
(Then, you have provided an example table with composite names and their corresponding material percentages.)
(Actually, the weight percentage of Material D is omitted, so it's unnecessary to ensure that the sum of a row is not 100.)
When x and y are small numbers, I can create the table manually using Excel. However, if x and y are large numbers, I cannot do it manually. Therefore, I would like to know Python code that can efficiently generate the table, regardless of the values of x and y.
英文:
I am trying to make a comprehensive compositon table for composite development (in chemistry field) based on the specific rules.
The rules are
- The numbers in a table mean the weight percentage of each raw material in the ccomposite
- There are three raw materials(A, B, C), and I can use up to two materials to make a composite
- Each number in a table must be a multiple of x and the sum of each row must be less than or equal to y
- I have to write every possible combination which meets the rules above.
For example, if x=1 and y=2, I can make a table shown below.
Material A Material B Material C
Composite1 0 0 0
Composite2 1 0 0
Composite3 0 1 0
Composite4 0 0 1
Composite5 1 1 0
Composite6 1 0 1
Composite7 0 1 1
Composite8 2 0 0
Composite9 0 2 0
Composite10 0 0 2
(Actually, the weight percentage of Material D is omitted, so it's unnecessary to think that the sum of a row is not 100.)
When x and y are small numbers I can make the table manually (using Excel), but if x and y are big numbers I can't.
Thus, I would like to know python codes which make the table efficiently regardless of x and y.
答案1
得分: 1
你可以使用 itertools.product
然后使用 列表推导 来筛选超出限制的组合。
import itertools
def get_compositions(raw_materials:list, factor_x:int,lim_y:int,col_prefix='Material '):
multiples = [factor_x*i for i in range(int(lim_y/factor_x)+1)]
cart_prod = itertools.product(multiples, repeat=len(raw_materials))
composites = [c for c in cart_prod if sum(c)<=lim_y] ## filter
composite_labels = [f'Composite{i}' for i in range(1, len(composites)+1)]
material_labels = [f'{col_prefix}{m}' for m in raw_materials]
return pd.DataFrame(composites, index=composite_labels, columns=material_labels)
get_compositions(['A', 'B', 'C'], 1, 2)
应返回
英文:
You could use itertools.product
and then filter out the compositions that exceed the limit with list comprehension.
import itertools
def get_compositions(raw_materials:list, factor_x:int,lim_y:int,col_prefix='Material '):
multiples = [factor_x*i for i in range(int(lim_y/factor_x)+1)]
cart_prod = itertools.product(multiples, repeat=len(raw_materials))
composites = [c for c in cart_prod if sum(c)<=lim_y] ## filter
composite_labels = [f'Composite{i}' for i in range(1, len(composites)+1)]
material_labels = [f'{col_prefix}{m}' for m in raw_materials]
return pd.DataFrame(composites, index=composite_labels, columns=material_labels)
<kbd>get_compositions(['A', 'B','C'], 1, 2)
</kbd> should return
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论