Python Forum
Combine nested lists in output - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Combine nested lists in output (/thread-8375.html)



Combine nested lists in output - python12345 - Feb-17-2018

TASK:

rhList = [[4.276, 7.000], [4.000, 8.000], [3.771, 9.000], \
[4.443, 3.142], [3.142, 6.284], [2.565, 9.425], [2.221, 12.566]]
rhList is given
Write a loop that does the following things:

Get the values for r and h from rhList:
Calculate O and V
Write out r, h, O og V with 3 decimales accuracy.
Save r, h, O og V in a nested list called resultList.
It should look like this when its done:
[[4.276, 7.0, 302.951168611585, 402.09003098979963],
[4.0, 8.0, 301.59289474462014, 402.1238596594935],
[3.771, 9.0, 302.5946920931822, 402.07349678766377],
[4.443, 3.142, 211.74431874567463, 194.85371033115726],
[3.142, 6.284, 186.08590738460174, 194.8939736674729],
[2.565, 9.425, 193.23528005185852, 194.80763343051234],
[2.221, 12.566, 206.35191318586456, 194.7350135716788]]

My first try is:

rhList = [[4.276, 7.000], [4.000, 8.000], [3.771, 9.000], \
[4.443, 3.142], [3.142, 6.284], [2.565, 9.425], [2.221, 12.566]]

ListO=[2*pi*r**2+2*pi*r*h for r,h in rhList]
ListV=[pi*r**2*h for r,h in rhList]
resultlist=[[O,V,r,h] for O,V in zip (ListO,ListV)]
resultlist

Outcome :
[[302.951168611585, 402.09003098979963, 2.221, 12.566],
[301.59289474462014, 402.1238596594935, 2.221, 12.566],
[302.5946920931822, 402.07349678766377, 2.221, 12.566],
[211.74431874567463, 194.85371033115726, 2.221, 12.566],
[186.08590738460174, 194.8939736674729, 2.221, 12.566],
[193.23528005185852, 194.80763343051234, 2.221, 12.566],
[206.35191318586456, 194.7350135716788, 2.221, 12.566]]

I dont know how to get r,h from rhList to also run the loop. They stay the same, also how do i only get 3 decimales in this case?

my second attempt:

rhList = [[4.276, 7.000], [4.000, 8.000], [3.771, 9.000], \
[4.443, 3.142], [3.142, 6.284], [2.565, 9.425], [2.221, 12.566]]

for r,h in rhList:
O=2*pi*r**2+2*pi*r*h
V=pi*r**2*h


print ('{0:0.3f} {1:7.3f} {2:10.3f} {3:15.3f}'.format(O,V,r,h))

outcome:
302.951 402.090 4.276 7.000
301.593 402.124 4.000 8.000
302.595 402.073 3.771 9.000
211.744 194.854 4.443 3.142
186.086 194.894 3.142 6.284
193.235 194.808 2.565 9.425
206.352 194.735 2.221 12.566

Now the results are correct, but they dont come out as an nested list.


RE: Combine nested lists in output - metulburr - Feb-17-2018

Quote:Now the results are correct, but they dont come out as an nested list.
Does the results have to be in string or float?

Instead of printing the formatted string at the end....put it in a list and finally append that list to a new empty list resultList. If you can use strings that will suffice, otherwise your going to have to convert the strings to floats first via float() after formatted.


RE: Combine nested lists in output - python12345 - Feb-17-2018

It doesnt say, but i think the results should be float.
In my first try O and V are running trough the loop and get out correct while r,h from rhList isnt. Why doesnt r,h also come out correct?