如何使用两个列表将函数应用于列表

2024-06-01 03:50:23 发布

您现在位置:Python中文网/ 问答频道 /正文

def windChillIndex(windSpeed,temp):
    windSpeedInKmH = windSpeed * 3.6
    WCI = 1.1626*((5.2735*(windSpeedInKmH**0.5))+10.45-(0.2778*windSpeedInKmH))*(30-temp)
    return WCI

windSpeed = []
temp = []
count = 0

while (count<1):
    windSpeed.append(float(raw_input("Enter a wind speed in meters per second.")))
    temp.append(float(raw_input("Enter a temperature.")))
    count += 1

for index1, object in enumerate(windSpeed):
    windSpeed[index1] = windChillIndex(object)

out = []
for object in windSpeed:
    out.append(windChillIndex(object))

print out

Tags: inforinputrawobjectcountfloatout
2条回答

有一个内置的!map将使用列表之类的iterables项调用函数。两张单子进去,一张单子出来。你知道吗

def windChillIndex(windSpeed,temp):
    windSpeedInKmH = windSpeed * 3.6
    WCI = 1.1626*((5.2735*(windSpeedInKmH**0.5))+10.45-(0.2778*windSpeedInKmH))*(30-temp)
    return WCI

windSpeed = []
temp = []
count = 0

while (count<1):
    windSpeed.append(float(raw_input("Enter a wind speed in meters per second.")))
    temp.append(float(raw_input("Enter a temperature.")))
    count += 1

out = map(windSpeedIndex, windSpeed, temp)
print out

您是否尝试迭代两个风速值和温度值列表,并生成一个新的风寒值列表?你知道吗

如果是这样,您应该使用内置的zip函数。这将获取两个列表并一起迭代,为您提供指向每个值的指针。你知道吗

for w, t in zip(windSpeed, temp):
    out.append(windChillIndex(w, t))

相关问题 更多 >