Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Newbie question to show nested namedtuple list
#1
Hello:
I have some code, which is not finished yet:
import collections

Sales = collections.namedtuple('Sales', ['ID', 'QuarterNo', 'QuarterSales', 'TotalSales'])
Q1Sales = collections.namedtuple('Q1Sales', ['ID', 'QuarterNo', 'QuarterSales'])
lSales = []
lQ1Sales = []

sale1_1 = Sales(ID = 1, QuarterNo = 1, QuarterSales = 100, TotalSales = 100) 
sale1_2 = Sales(ID = 1, QuarterNo = 2, QuarterSales = 200, TotalSales = 300) 
sale1_3 = Sales(ID = 1, QuarterNo = 3, QuarterSales = 300, TotalSales = 600) 
sale1_4 = Sales(ID = 1, QuarterNo = 4, QuarterSales = 400, TotalSales = 1000) 
sale2_1 = Sales(ID = 2, QuarterNo = 1, QuarterSales = 10, TotalSales = 10) 
sale2_2 = Sales(ID = 2, QuarterNo = 2, QuarterSales = 20, TotalSales = 30) 
sale2_3 = Sales(ID = 2, QuarterNo = 3, QuarterSales = 30, TotalSales = 60) 
sale2_4 = Sales(ID = 2, QuarterNo = 4, QuarterSales = 40, TotalSales = 100) 
sale3_1 = Sales(ID = 3, QuarterNo = 1, QuarterSales = 30, TotalSales = 30) 
sale3_2 = Sales(ID = 3, QuarterNo = 2, QuarterSales = 400, TotalSales = 430) 
sale3_3 = Sales(ID = 3, QuarterNo = 3, QuarterSales = 500, TotalSales = 930) 
sale3_4 = Sales(ID = 3, QuarterNo = 4, QuarterSales = 600, TotalSales = 1530)
lSales.append(sale1_1)
lSales.append(sale1_2)
lSales.append(sale1_3)
lSales.append(sale1_4)
lSales.append(sale2_1)
lSales.append(sale2_2)
lSales.append(sale2_3)
lSales.append(sale2_4)
lSales.append(sale3_1)
lSales.append(sale3_2)
lSales.append(sale3_3)
lSales.append(sale3_4)

Q1Sale1_1 = Q1Sales(ID = 1, QuarterNo = 1, QuarterSales = 100) 
Q1Sale1_2 = Q1Sales(ID = 1, QuarterNo = 2, QuarterSales = 200) 
Q1Sale1_3 = Q1Sales(ID = 1, QuarterNo = 3, QuarterSales = 300) 
Q1Sale1_4 = Q1Sales(ID = 1, QuarterNo = 4, QuarterSales = 400) 
Q1Sale2_1 = Q1Sales(ID = 2, QuarterNo = 1, QuarterSales = 10) 
Q1Sale2_2 = Q1Sales(ID = 2, QuarterNo = 2, QuarterSales = 20) 
Q1Sale2_3 = Q1Sales(ID = 2, QuarterNo = 3, QuarterSales = 30) 
Q1Sale2_4 = Q1Sales(ID = 2, QuarterNo = 4, QuarterSales = 40) 
Q1Sale3_1 = Q1Sales(ID = 3, QuarterNo = 1, QuarterSales = 30) 
Q1Sale3_2 = Q1Sales(ID = 3, QuarterNo = 2, QuarterSales = 400) 
Q1Sale3_3 = Q1Sales(ID = 3, QuarterNo = 3, QuarterSales = 500) 
Q1Sale3_4 = Q1Sales(ID = 3, QuarterNo = 4, QuarterSales = 600) 
lQ1Sales.append(Q1Sale1_1)
lQ1Sales.append(Q1Sale1_2)
lQ1Sales.append(Q1Sale1_3)
lQ1Sales.append(Q1Sale1_4)
lQ1Sales.append(Q1Sale2_1)
lQ1Sales.append(Q1Sale2_2)
lQ1Sales.append(Q1Sale2_3)
lQ1Sales.append(Q1Sale2_4)
lQ1Sales.append(Q1Sale3_1)
lQ1Sales.append(Q1Sale3_2)
lQ1Sales.append(Q1Sale3_3)
lQ1Sales.append(Q1Sale3_4)

lSales.sort(key=lambda tup: -tup[3])
print(lSales[0])
For the employees who have achived the sales records, I want to sort them with total sales, and show the people with the maximum total sale, but I want to show his quarterly sales from
nanedtuple: Q1Sales. Some kind of nested namedtuples list.
Please advise on how to show the best salesman's quarterly results (lQ1Sales).
Quote
#2
A dict would probably be more appropriate in the case. You could map salesperson to sales. But perhaps that violates a constant... Is this an assignment?
Quote
#3
You can sort the nametuple list like this.
>>> import collections
>>> Sales = collections.namedtuple('Sales', ['ID', 'QuarterNo', 'QuarterSales', 'TotalSales'])
>>> Q1Sales = collections.namedtuple('Q1Sales', ['ID', 'QuarterNo', 'QuarterSales'])
>>> lSales =
>>> sale1_1 = Sales(ID = 1, QuarterNo = 1, QuarterSales = 100, TotalSales = 100)
>>> sale1_2 = Sales(ID = 1, QuarterNo = 2, QuarterSales = 200, TotalSales = 300) 
>>> sale3_4 = Sales(ID = 3, QuarterNo = 4, QuarterSales = 600, TotalSales = 1530)
>>> lSales.append(sale1_1)
>>> lSales.append(sale1_2)
>>> lSales.append(sale3_4)
>>> lSales
[Sales(ID=1, QuarterNo=1, QuarterSales=100, TotalSales=100),
 Sales(ID=1, QuarterNo=2, QuarterSales=200, TotalSales=300),
 Sales(ID=3, QuarterNo=4, QuarterSales=600, TotalSales=1530)]

>>> sorted(lSales, key=lambda lSales: lSales.TotalSales, reverse=True)
[Sales(ID=3, QuarterNo=4, QuarterSales=600, TotalSales=1530),
 Sales(ID=1, QuarterNo=2, QuarterSales=200, TotalSales=300),
 Sales(ID=1, QuarterNo=1, QuarterSales=100, TotalSales=100)]

>>> sorted(lSales, key=lambda lSales: lSales.TotalSales, reverse=True)[0].TotalSales
1530
Quote
#4
(Dec-18-2017, 10:53 PM)mpd Wrote: A dict would probably be more appropriate in the case. You could map salesperson to sales. But perhaps that violates a constant... Is this an assignment?
Hello,
As I am rather new for python programming, could you show me the code what you can do this with a dict?
Thanks,
Quote
#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.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Newbie Question juljan 4 150 Jan-13-2020, 03:07 AM
Last Post: snippsat
  Newbie question... mrochac 2 145 Nov-14-2019, 02:55 PM
Last Post: Larz60+
  namedtuple help anna 5 289 Oct-19-2019, 05:31 AM
Last Post: buran
  list approach due nested order 3Pinter 6 212 Oct-07-2019, 01:49 PM
Last Post: 3Pinter
  Newbie question Shadowlord 2 230 Sep-30-2019, 12:52 PM
Last Post: Shadowlord
  convert List of Dicts into a 2 deep Nested Dict rethink 1 408 Aug-23-2019, 05:28 PM
Last Post: ichabod801
  Definition of .format? (Newbie question) Jan_97 4 450 Jul-13-2019, 12:49 PM
Last Post: Jan_97
  Count the number of items in a nested list iofhua 2 401 Jun-07-2019, 06:47 PM
Last Post: iofhua
  reverse list, incl. nested list Livne_ye 3 400 May-04-2019, 12:34 PM
Last Post: Livne_ye
  Newbie Question re "TypeError: '<' not supported between instances of 'list' and 'int sr12 8 1,694 Apr-11-2019, 08:19 PM
Last Post: sr12

Forum Jump:


Users browsing this thread: 1 Guest(s)