Q:

Python program to apply lambda functions on array

belongs to collection: Python Array Programs

0

Let's suppose a real-life scenario in which you have a website that displays the temperature of multiple cities. Here, you get data of temperature of all cities in a collection and conversion can be made easy using lambda functions.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Python program to convert temperature stored in an array using regular function

# Python program to convert temperature to 
# Fahrenheit using function

# Function that converts temperature to Farenhiet
def convCtoF(c):
    f=9/5*c+32
    return f

# Array storing Temperature in celsius
celsiusTemperature =[12,45,6,78,5,26,67]
print("Temperature in celsius : ",celsiusTemperature)

# Array that will store Temperature in Fahrenheit
FahrenheitTemperature = []
for t in celsiusTemperature:
    x = convCtoF(t)
    FahrenheitTemperature.append(x)
# Printing Fahrenheit Temperature 
print("Temperature in Fahrenheit : ",FahrenheitTemperature)

Output:

Temperature in celsius :  [12, 45, 6, 78, 5, 26, 67]
Temperature in Fahrenheit :  [53.6, 113.0, 42.8, 172.4, 41.0, 78.80000000000001, 152.60000000000002]

Python program to convert temperature to Fahrenheit using lambda function

# Python program to convert temperature to 
# Fahrenheit using lambda function 

# Array storing temperature in celsius
celsiusTemp = [12,45,6,78,5,26,67]
print("temperature in Celsius : ",celsiusTemp)

# Converting data to Fahrenheit using lambda function 
fahrenTemp = list(map(lambda c:9/5*c+32,celsiusTemp))
print("Temperature in Fahrenheit : ",fahrenTemp)

Output:

temperature in Celsius :  [12, 45, 6, 78, 5, 26, 67]
Temperature in Fahrenheit :  [53.6, 113.0, 42.8, 172.4, 41.0, 78.80000000000001, 152.60000000000002]

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python program to find the GCD of the array... >>
<< Python program to illustrate the working of lambda...