PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, November 5, 2022

[FIXED] How would I Convert the list below from Celsius to Fahrenheit, using the map function with a lambda?

 November 05, 2022     dictionary, function, lambda, list, python     No comments   

Issue

I have been having a hard time trying to convert this list because it has a tuple inside and manipulating tuples isn't easy because essentially they're unchangeable. But apparently there are ways around it because I was able to adjust the numbers, however I am not able to bring back a string as well to make a new list. I receive this error when I try:

  Traceback (most recent call last):
  File "/Users/Swisha/Documents/Coding Temple/Python VI/tupe.py", line 4, in <module>
  newplaces = list(map(lambda c:  c[0] (9/5) * c[1]+ 32, places))
  File "/Users/Swisha/Documents/Coding Temple/Python VI/tupe.py", line 4, in <lambda>
  newplaces = list(map(lambda c:  c[0] (9/5) * c[1]+ 32, places))
  TypeError: 'str' object is not callable

My attempt with the error:

# F = (9/5)*C + 32
places = [('Nashua',32),("Boston",12),("Los Angelos",44),("Miami",29)]

newplaces = list(map(lambda c: c[0] (9/5) * c[1]+ 32, places))
print(newplaces)

My output without the strings:

[89.6, 53.6, 111.2, 84.2]

The required output:

[('Nashua', 89.6), ('Boston', 53.6), ('Los Angelos', 111.2), ('Miami', 84.2)]

Solution

c is a tuple (c[0] is the place name), so

# return a tuple that includes the place name
newplaces = list(map(lambda c:  (c[0], (9/5) * c[1]+ 32), places))
print(newplaces)
[('Nashua', 89.6), ('Boston', 53.6), ('Los Angelos', 111.2), ('Miami', 84.2)]


Answered By - cottontail
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing