Reputation: 573
In the following python script, it converts the Celsius degree to Fahrenheit but I need to join two list with strings between and after them
Celsius = [39.2, 36.5, 37.3, 37.8]
fahrenheit = map(lambda x: (float(9)/5)*x + 32, Celsius)
print '\n'.join(str(i) for i in Celsius)+" in Celsius is "+''.join(str(i) for i in fahrenheit )+" in farenheit"
The outcome is this(not what i wanted):
39.2
36.5
37.3
37.8 in Celsius is 102.5697.799.14100.04 in farenheit
How can I achieve this:
39.2 in Celsius is equivalent to 102.56 in fahrenheit
36.5 in Celsius is equivalent to 97.7 in fahrenheit
37.3 in Celsius is equivalent to 99.14 in fahrenheit
37.8 in Celsius is equivalent to 100.04 in fahrenheit
EDIT SORRY MY BAD Well, the original code I had was
def fahrenheit(T):
return ((float(9)/5)*T + 32)
def display(c,f):
print c, "in Celsius is equivalent to ",\
f, " in fahrenheit"
Celsius = [39.2, 36.5, 37.3, 37.8]
for c in Celsius:
display(c,fahrenheit(c))
But due to reasons I need it to be within 3 lines
Upvotes: 2
Views: 122
Reputation: 309831
It's probably easiest to do the formatting as you go:
Celsius = [39.2, 36.5, 37.3, 37.8]
def fahrenheit(c):
return (float(9)/5)*c + 32
template = '{} in Celsius is equivalent to {} in fahrenheit'
print '\n'.join(template.format(c, fahrenheit(c)) for c in Celsius)
EDIT
If you really want it under 3 lines, we can inline the fahrenheit
function:
Celsius = [39.2, 36.5, 37.3, 37.8]
template = '{} in Celsius is equivalent to {} in fahrenheit'
print '\n'.join(template.format(c, (float(9)/5)*c + 32) for c in Celsius)
If you don't mind long lines, you could inline template
as well and get it down to 2 lines...
However, there really isn't any good reason to do this as far as I can tell. There is no penalty for writing python code that takes up more lines. Indeed, there is generally a penalty in the other direction that you pay every time you try to understand a really long complex line of code :-)
Upvotes: 8
Reputation: 20178
3 lines:
>>> Celsius = [39.2, 36.5, 37.3, 37.8]
>>> msg = '%g in Celsius is equivalent to %g in Fahrenheit'
>>> print '\n'.join(msg % (c, (9. * c)/5. + 32.) for c in Celsius)
yields:
39.2 in Celsius is equivalent to 102.56 in Fahrenheit
36.5 in Celsius is equivalent to 97.7 in Fahrenheit
37.3 in Celsius is equivalent to 99.14 in Fahrenheit
37.8 in Celsius is equivalent to 100.04 in Fahrenheit
Upvotes: 2
Reputation: 6680
In order to do it with join
, you can include the extra parts of the string inside of the join
statement.
celsius = [39.2, 36.5, 37.3, 37.8]
fahrenheit = map(lambda x: (float(9)/5)*x + 32, Celsius)
print '\n'.join(str(i) + " in celsius is " + str(j) + "in farenheit" for i, j in zip(celsius, fahrenheit))
Upvotes: 2
Reputation: 50
You could always try formatting using %f for float numbers and placing the output in a loop:
for i in range(len(Celsius)):
print '%f in Celsius is equivalent to %f in fahrenheit' % (Celsius[i], fahrenheit[i])
EDIT: As per the suggestion of @mgilson, using zip
would be better to use instead of taking a count of just Celsius
:
for c,f in zip(Celsius, fahrenheit):
print '%f in Celsius is equivalent to %f in fahrenheit' % (c,f)
Upvotes: 0