Python Forum
Newbie question to show nested namedtuple list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Newbie question to show nested namedtuple list
#5
On further reflection, you probably just want the QuarterSales field of the Sales namedtuple to be a list of QuarterlySales objects:

QuarterlySales = collections.namedtuple('QuarterlySales', ['ID', 'QuarterNo', 'QuarterSales'])
Sales = collections.namedtuple('Sales', ['ID', 'QuarterSales', 'TotalSales'])

sales1 = Sales(1, [QuarterlySales (1, 1, 100), QuarterlySales (2, 2, 200), ...], 350)
If you want to go the dict route...


A dictionary (dict) is a data structure that enables you to map keys to values. So in your case, I would do something like the following.
https://docs.python.org/3/tutorial/datas...ctionaries

Sales = namedtuple(...)
QuarterlySales = namedtuple(...)  

sales = {}  # sales is an empty dict.  The dict will map salesperson ids to lists of sales
sales[1] = [ QuarterlySales(...), QuarterlySales(...), ...]  # so sales person with ID 1 gets these QuarterlySales tuples...
sales[2] = [ ... ]

#  So now when you figure out which sales person has the highest total, you can just do something like this:
best_salesperson = # however you figure that out
quarterlies = sales[best_salesperson.ID]  # quarterlies is a list of QuarterlySales 
This is all sketchy sample/pseudo-code to get you started.
Reply


Messages In This Thread
RE: Newbie question to show nested namedtuple list - by mpd - Dec-19-2017, 12:55 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How is pandas modifying all rows in an assignment - python-newbie question markm74 1 704 Nov-28-2023, 10:36 PM
Last Post: deanhystad
  newbie question - can't make code work tronic72 2 694 Oct-22-2023, 09:08 PM
Last Post: tronic72
  Newbie question about switching between files - Python/Pycharm Busby222 3 614 Oct-15-2023, 03:16 PM
Last Post: deanhystad
  Newbie.... run for cover. OpenCV question Stevolution2023 2 987 Apr-12-2023, 12:57 PM
Last Post: Stevolution2023
  List all possibilities of a nested-list by flattened lists sparkt 1 922 Feb-23-2023, 02:21 PM
Last Post: sparkt
  numpy newbie question bcwilly_ca 4 1,192 Feb-10-2023, 05:55 PM
Last Post: jefsummers
  Сheck if an element from a list is in another list that contains a namedtuple elnk 8 1,852 Oct-26-2022, 04:03 PM
Last Post: deanhystad
  PIL Image im.show() no show! Pedroski55 2 974 Sep-12-2022, 10:19 PM
Last Post: Pedroski55
  Updating nested dict list keys tbaror 2 1,290 Feb-09-2022, 09:37 AM
Last Post: tbaror
  PIL Image im.show() no show! Pedroski55 6 4,942 Feb-08-2022, 06:32 AM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

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