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

时间:2015-12-03 02:43:44

标签: python ipython ipython-notebook

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

2 个答案:

答案 0 :(得分:0)

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

如果是这样,您应该使用内置的zip功能。这将采用两个列表并一起迭代它们,为您提供指向每个值的指针。

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

答案 1 :(得分:0)

有一个内置的! map将使用列表等可迭代项调用函数。 2个列表进入,一个列表出来。

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