Python Forum
Combine nested lists in output
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Combine nested lists in output
#1
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.
Reply
#2
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.
Recommended Tutorials:
Reply
#3
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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sorting nested lists in ascending order jszum 2 2,244 May-17-2020, 01:35 PM
Last Post: jefsummers
  Recursions with nested lists sashiessay 2 2,766 Oct-05-2019, 11:40 AM
Last Post: sashiessay
  Sum of Nested Lists raifuru42 2 4,335 Feb-19-2018, 02:57 PM
Last Post: mckingstar
  Nested loops, lists and if statements Liquid_Ocelot 10 8,930 Apr-23-2017, 02:02 PM
Last Post: Mekire

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020