1

This is my dataset:

Sales = [
["101TGY" , "George" , "Taylor" , 6009 , 5262 , 3745 , 7075 , 1943 , 4432],
["103FCY" , "Fehlix" , "Chayne" , 8717 , 2521 , 5777 , 6189 , 5089 , 6957],
["102SBY" , "Sumren" , "Bergen" , 5012 , 1063 , 7937 , 9560 , 1115 , 5499],
["104SBK" , "Samira" , "Beckle" , 1140 , 9206 , 3898 , 8544 , 5937 , 8705],
["105NBT" , "Nellie" , "Bogart" , 3017 , 3342 , 5939 , 2479 , 3374 , 2297],
["106CGT" , "Cheryl" , "Grouth" , 9620 , 7160 , 5113 , 4803 , 5492 , 2195],
["107DGT" , "Danuta" , "Graunt" , 1583 , 7450 , 1026 , 7463 , 2390 , 6509],
["108JDN" , "Jaiden" , "Deckle" , 4064 , 4978 , 2984 , 3159 , 1464 , 4858],
["109JCK" , "Jimran" , "Caliks" , 6253 , 7962 , 2732 , 7504 , 2771 , 5193],
["110DDN" , "Deynar" , "Derran" , 6305 , 8817 , 5200 , 3647 , 3365 , 1256]]

for i in Sales:
    #do something

How can I add up the number of sales (the integers) of each person?

Notice how the number of columns could change. Meaning the code should also work with this dataset:

Sales = [
["101TGY" , "George" , "Taylor" , "absent", 6009 , 5262 , 3745 , 7075 , 1943 , 4432, 3455],
["103FCY" , "Fehlix" , "Chayne" , 8717 , 2521 , 5777 , 6189 , 5089 , 6957],
["102SBY" , "Sumren" , "Bergen" , 5012 , 1063 , 7937 , 9560 , 1115 , 5499, 345],
["104SBK" , "Samira" , "Beckle" , "absent", 1140 , 9206 , 3898 , 8544 , 5937],
["105NBT" , "Nellie" , "Bogart" , 3017 , 3342 , 5939 , 2479 , 3374 , 2297, 8723],
["106CGT" , "Cheryl" , "Grouth" , 9620 , 7160 , 5113 , 4803 , 5492 , 2195],
["107DGT" , "Danuta" , "Graunt" , 1583 , 7450 , 1026 , 7463 , 2390 , 6509],
["108JDN" , "Jaiden" , "Deckle" , 4064 , 4978 , 2984 , 3159 , 1464 , 4858, 3223],
["109JCK" , "Jimran" , "Caliks" , 6253 , 7962 , 2732 , 7504 , 2771 , 5193],
["110DDN" , "Deynar" , "Derran" , "absent", 6305 , 8817 , 5200 , 3647 , 3365 , 1256]]

I've tried doing:

for i in Sales:
    total = i[3]+i[4]+i[5]+i[6]+i[8]
    print(total)

which works for the first dataset however not for the second, which is an issue.

  • You can use a tryparse as seen here: https://stackoverflow.com/questions/2262333/is-there-a-built-in-or-more-pythonic-way-to-try-to-parse-a-string-to-an-integer which can be 'map' to the list then you can wrap the result with a sum function – Bing Wang Feb 13 '21 at 18:53
  • You have an inconsistent number of "columns" in each table, see `"absent"` – bherbruck Feb 13 '21 at 18:54

3 Answers3

3

You could sum all the integer values for each sales line

for line in Sales:
    total = sum(i for i in line if isinstance(i, int))
    print(total)

31921
35250
30531
28725
29171
34383
26421
24730
32415
28590
ScootCork
  • 3,411
  • 12
  • 22
1
sum(i[3] for i in Sales if isinstance(i[3],int) )

38266

Your Sales list has nested lists within. within that sublist we need to add the 3 element and we need to check if it is an int.

Sum --> i[3] ||for i in Sales || if isinstance(i[3],int)

for i in Sales --> For every `sublist` in Sales .
i[3]           --> In sublist I'm interested in `3` element. So i[3].
if isinstance(i[3],int) --> I only need the integers.

isinstance(object, classinfo)

Return True if the object argument is an instance of the classinfo argument, or of a (direct, indirect or virtual) subclass thereof.
Ajay
  • 5,267
  • 2
  • 23
  • 30
  • 1
    The community encourages adding explanations alongisde code, rather than purely code-based answers (see [here](https://meta.stackoverflow.com/questions/300837/what-comment-should-i-add-to-code-only-answers)). – costaparas Feb 14 '21 at 04:52
  • @costaparas .hey added...I thought it's intuitive, so didn't add..thanks for the reminder.I've updated – Ajay Feb 14 '21 at 08:22
  • Great, this had come up while reviewing [LQA](https://stackoverflow.com/review/low-quality-posts/28317254), probably flagged automatically since it didn't have enough text in it. Shorter posts tend to end up in that queue. Now, with the explanation, it helps future visitors more, and its worth upvoting. – costaparas Feb 14 '21 at 09:42
0

The below code should do:

total = [0 for i in range(len(Sales))]
count = 0
for i in Sales:
    for j in i:
        if(type(j) == int):
             total[count] += j    
    count+=1
print(total)
Wis124
  • 27
  • 4