如何高效制作全面的组合表

huangapple go评论73阅读模式
英文:

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=&#39;Material &#39;):
    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)&lt;=lim_y] ## filter

    composite_labels = [f&#39;Composite{i}&#39; for i in range(1, len(composites)+1)]
    material_labels = [f&#39;{col_prefix}{m}&#39; for m in raw_materials]

    return pd.DataFrame(composites, index=composite_labels, columns=material_labels)

<kbd>get_compositions([&#39;A&#39;, &#39;B&#39;,&#39;C&#39;], 1, 2)</kbd> should return

> 如何高效制作全面的组合表

huangapple
  • 本文由 发表于 2023年4月4日 05:21:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75923863.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定