【问题标题】:Generate all possible outcomes from lists with two different functions从具有两种不同功能的列表中生成所有可能的结果
【发布时间】:2021-05-12 08:03:15
【问题描述】:

总的来说,我对编程很陌生。我正在使用 Python,但遇到了一些困难。

我有两个函数,每个函数都有 4 个变量,它们都包含相同的变量。对于变量,我创建了 4 个具有不同变量值的列表。 现在我希望 Python 计算/生成这两个函数的所有可能结果。

随后,我想将这些结果绘制在散点图中,但现在有人可以帮助我解决这个问题。

我必须尝试列出所有组合,但我无法将其链接到 2 个函数。 所以x1 可以是678x2 可以是 456x3 可以是235

提前非常感谢!

#def heat transmission
def BIC (x1, x2, x3, x4):
    return (75 / x1) + (85 / x2) + (55 / x3) + (12 * x4)
    

#def TOjuli
def TOjuli(x1, x2, x3, x4):
    return ((400 * 1000) / ((75 / x1) + (85 / x2) + (55 / x3) + (12 * x4)))
    


#variables

# Different modules
x1_options = [6, 7, 8]
x2_options = [4, 5, 6]
x3_options = [2, 3, 5]
x4_options = [0.5, 0.4, 0.6]

【问题讨论】:

  • btw 你的函数都返回None。您的意思是(对于第一个函数):return (75 / x1) + (85 / x2) + (55 / x3) + (12 * x4)
  • 是的,这就是我的意思(我想)我已经编辑了代码

标签: python list function


【解决方案1】:

所以也许你打算这样做:

import itertools

def BIC (x1, x2, x3, x4):
    return (75 / x1) + (85 / x2) + (55 / x3) + (12 * x4)

#def TOjuli
def TOjuli(x1, x2, x3, x4):
    return ((400 * 1000) / ((75 / x1) + (85 / x2) + (55 / x3) + (12 * x4)))

x1_options = [6, 7, 8]
x2_options = [4, 5, 6]
x3_options = [2, 3, 5]
x4_options = [0.5, 0.4, 0.6]

''' UPDATE: print a selection of the combinations of options '''
combinations = list(itertools.product(x1_options, x2_options, x3_options, x4_options))
for c in combinations:
    print(c, BIC(*c))

输出:(前 10 行)

(6, 4, 2, 0.5) 67.25
(6, 4, 2, 0.4) 66.05
(6, 4, 2, 0.6) 68.45
(6, 4, 3, 0.5) 58.08333333333333
(6, 4, 3, 0.4) 56.883333333333326
(6, 4, 3, 0.6) 59.28333333333333
(6, 4, 5, 0.5) 50.75
(6, 4, 5, 0.4) 49.55
(6, 4, 5, 0.6) 51.95
(6, 5, 2, 0.5) 63.0

【讨论】:

  • 我想找到所有可能的组合/结果,所以在这两个函数中。 { combination = list(itertools.product(x1_options, x2_options, x3_options, x4_options)) print(combinations) } 如果我使用这行代码,例如,它会显示所有可能的组合,但现在我想在函数中计算它们.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-08
  • 1970-01-01
  • 2021-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多