Python Forum
creating a list from lists of lists
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
creating a list from lists of lists
#2
Try not copy in line number in code.

So this is the start.
>>> lst = main()
>>> lst
[[[2, 1, 4, 3], [3, 4, 2, 1]],
 [[1, 0, 0, 3], [0, 3, 1, 1]],
 [[0, 1, 4, 2], [4, 0, 1, 3]],
 [[1, 0, 0, 4], [3, 3, 0, 0]],
 [[3, 1, 3, 0], [3, 4, 4, 2]]]
So to use eg sum() on the list need first to flatten the list out.
>>> for i in lst:
...     lst = [item for n in i for item in n]
...     print(lst)   
...     
[2, 1, 3, 4, 1, 4, 3, 4]
[4, 0, 1, 3, 0, 3, 0, 0]
[0, 0, 0, 1, 2, 1, 4, 0]
[2, 1, 0, 1, 4, 1, 4, 3]
[4, 0, 3, 4, 1, 4, 2, 2]
Now will sum() work.
>>> lst = main()
>>> for i in lst:
...     lst = [item for n in i for item in n]
...     print(sum(lst))     
...     
17
18
13
12
15
Then i think you can do the last step if want the total sum.
Reply


Messages In This Thread
RE: creating a list from lists of lists - by snippsat - Sep-13-2019, 05:22 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question Using Lists as Dictionary Values bfallert 8 332 Apr-21-2024, 06:55 AM
Last Post: Pedroski55
  problem with print lists MarekGwozdz 4 696 Dec-15-2023, 09:13 AM
Last Post: Pedroski55
  python convert multiple files to multiple lists MCL169 6 1,566 Nov-25-2023, 05:31 AM
Last Post: Iqratech
  Lists blake7 6 772 Oct-06-2023, 12:46 PM
Last Post: buran
  Trying to understand strings and lists of strings Konstantin23 2 770 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  Why do the lists not match? Alexeyk2007 3 820 Jul-01-2023, 09:19 PM
Last Post: ICanIBB
  ''.join and start:stop:step notation for lists ringgeest11 2 2,444 Jun-24-2023, 06:09 AM
Last Post: ferdnyc
  Need help with sorting lists as a beginner Realist1c 1 749 Apr-25-2023, 04:32 AM
Last Post: deanhystad
  Pip lists the module but python does not find it Dato 2 1,285 Apr-13-2023, 06:40 AM
Last Post: Dato
  Generate lists of devices and partitions from /proc/partitions? DachshundDigital 1 780 Feb-28-2023, 10:55 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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