gpt4 book ai didi

python - 具有三个列表的逻辑运算符

转载 作者:太空宇宙 更新时间:2023-11-03 17:22:28 24 4
gpt4 key购买 nike

我的列表有问题:我有三个列表:

Pipe_sizes = [15,15,22,15,32,45]
Flow_rates = [0.1,0.3,1,2,0.4,1.5]
Material_pipes = [Copper, Copper, PVC, Steel, Steel, Copper]

我想使用逻辑运算符来更改列表 Pipe_sizes,如下所示:

If the material is Copper, I will use the follow logical operators:
if Flow_rates <= 0.2 then the pipe size is 15
if Flow_rates > 0.2 and <= 1 then the pipe size is 22
if Flow_rates > 1 and <=1.9 then the pipe size is 32
if Flow_rates > 1.9 then the pipe size is 45

对于 PVC,我将使用以下逻辑运算符:

if Flow_rates <= 0.1 then the pipe size is 15
if Flow_rates > 0.1 and <= 1 then the pipe size is 22
if Flow_rates > 1 and <=1.4 then the pipe size is 32
if Flow_rates > 1.4 then the pipe size is 45

对于 Steel,我将使用以下逻辑运算符:

if Flow_rates <= 0.1 then the pipe size is 15
if Flow_rates > 0.1 and <= 0.8 then the pipe size is 22
if Flow_rates > 0.8 and <=1.5 then the pipe size is 32
if Flow_rates > 1.5 then the pipe size is 45

如果我没有 Material_pipes 列表,则可以轻松更改 Pipe_sizes 列表。我可以使用以下解决方案:

def flow_rate_to_size(rate):
if rate <= 0.2:
size = 15
elif 0.2 < rate <= 1:
size = 22
elif 1 < rate <= 1.9:
size = 32
else:
size = 45
return size

Flow_rates = [0.1, 0.3, 1, 2, 0.4, 1.5]
Pipe_sizes = [Flow_rate_to_size(rate) for rate in Flow_rates]
print(Pipe_sizes)

但是,如果 Pipe_sizes 也依赖于列表 Material_pipes,我该怎么办?

最佳答案

看看这是否是您要找的:

from bisect import bisect

material_pipes = {'Copper': (0.2, 1.0, 1.9),
'PVC': (0.1, 1, 1.4),
'Steel': (0.1, 0.8, 1.5)}
pipe_sizes = [15,22,32,45]

def flow_rate_to_size(material, rate):
pos = bisect(material_pipes[material], rate)
return pipe_sizes[pos]

示例:

print(flow_rate_to_size('Copper', 0.0))
print(flow_rate_to_size('Copper', 1.4))
print(flow_rate_to_size('Copper', 2.5))

产生:

15
32
45

关于python - 具有三个列表的逻辑运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33002971/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com