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 without 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)]
CodePudding user response:
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)]
CodePudding user response:
You should include the first element of the tuple also.
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)
CodePudding user response:
I suggest to use list comprehension, rather than functional map()
which is bad readability, if it is not learning purpose.
# F = (9/5)*C 32
places = [('Nashua',32),("Boston",12),("Los Angelos",44),("Miami",29)]
newplaces = [(place, (9/5) * c 32) for place, c in places]
print(newplaces)