简体   繁体   中英

Using List Comprehension and formulas

I'm trying to use list comprehension to covert degrees in Celsius over to degrees in Fahrenheit using the formula: c * 9/5 + 32 and I am confused as to what to do. I only got as far as outputting the list into a float.

Code so far:

degrees = [ 12, 21, 15, 32] 
farenheit = { float(degree) for degree in degrees } 
print(farenheit)

Where would I implement the formula c * 9/5 + 32 within the code?

Try this. I don't know why you tried to create a set instead of creating a list. Also I would say the float is unnecessary unless this is for visual purposes which I highly doubt.

degrees = [ 12, 21, 15, 32] 
fahrenheit = [degree * 9/5 + 32 for degree in degrees]
print(fahrenheit)

You can also do it using map function:

celsius=[12,21,15,32]
farenheit=list(map(lambda c: c*9/5+32,celsius))
print(farenheit)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM