[英]Logical operators with three lists
我的清单有问题:我有三个清单:
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
只需传递这两部分信息,然后对所传递的材料使用适当的信息即可。
def flow_rate_to_size(rate, material):
size = -1 # good to set an error state
if material.lower() == 'copper':
if rate <= 0.2:
size = 15
elif 0.2 < rate <= 1:
size = 22
elif 1 < rate <= 1.9:
size = 32
else:
size = 45
elif material.lower() == 'pvc':
if rate <= 0.1:
size = etc....
etc...
return size
hi guys....
I don;t understand why but it doesn't work...
have a look please...
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"]
def Flow_rate_to_size(rate,material):
size = -1 # good to set an error state
if material.lower() == 'Copper':
if rate <= 0.2:
size = 15
elif 0.2 < rate <= 1:
size = 22
elif 1 < rate <= 1.9:
size = 32
else:
size = 45
elif material.lower() == 'PVC':
if rate <= 0.1:
size = 15
elif 0.1 < rate <= 1:
size = 22
elif 1 < rate <= 1.4:
size = 32
else:
size = 45
elif material.lower() == 'Steel':
if rate <= 0.1:
size = 15
elif 0.1 < rate <= 0.8:
size = 22
elif 0.8 < rate <= 1.5:
size = 32
else:
size = 45
return size
Pipe_sizes = [Flow_rate_to_size(rate,material) for rate in Flow_rates]
print(Pipe_sizes)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.