Home > Mobile >  How do I add integers from a list of lists if the string in the list of lists is the same?
How do I add integers from a list of lists if the string in the list of lists is the same?

Time:03-29

I want to be able to add certain numbers in lists in a list together depending on if the first item in the lists within the list match. For example:

myList = [['Mar', 5], ['Feb', 29], ['Mar', 7], ['Jan', 20], ['Feb', 3], ['Feb', 4], ['Mar', 12], ['Jan', 2], ['Jan', 16]]

If the the first item in each list ('Mar', 'Feb', or 'Jan') match, I want to add the numbers of that list together and ultimately get a list of lists that looks like this:

newList = [['Mar', 24], ['Feb', 36], ['Jan', 38]]

However, I'm fairly new to programming and I'm not sure how to approach this. Any advice on how to achieve this would be greatly appreciated.

CodePudding user response:

If you have the list as an given input you can add up the values using a dictionary. If you can choose the data structure for the input yourself, consider using a dictionary straight away.

myList = [['Mar', 5], ['Feb', 29], ['Mar', 7], ['Jan', 20], ['Feb', 3], ['Feb', 4], ['Mar', 12], ['Jan', 2], ['Jan', 16]]

months = dict()
for [month, day] in myList:
    if month in months:
        months[month]  = day
    else:
        months[month] = day

# output dictionary (you may wanna use this for further computation)
print(months)

# if you need it sorted (and as a list)
sorted_month = sorted(months.items(), key=lambda item: item[1])
print(sorted_month)

Expected output:

{'Mar': 24, 'Feb': 36, 'Jan': 38}
[('Mar', 24), ('Feb', 36), ('Jan', 38)]

CodePudding user response:

it looks like you are using python, but i will answer in pseudocode anyway. In the future read stackoverflow help section to learn how to ask a good question, and tag appropriately

when coding, we are breaking down problems.

myList = [['Mar', 5], ['Feb', 29], ['Mar', 7], ['Jan', 20], ['Feb', 3], ['Feb', 4], ['Mar', 12], ['Jan', 2], ['Jan', 16]]

if you gave me the task physically, i might create a little tally, of each month and their current running total. (this is what @Mushroomator has provided code for)

another option is to search for the months im looking for and go through the list, finding a total for that specific month as i do. e.g. find jan total by going to first entry, discarding if not jan, adding to a total if jan, and then moving onto the next month

i actually have many more, think how would you break this down?

to code the second one:

monthsICareAbout = [‘jan’,’etc?’]
newList=[]
for month in monthsICareAbout
     running_total=0 (because its fresh per month)
     for data in myList
          if data[0] == month
              running_total  = data[1] (adding the number part)
      newList.add([month, running_total])

      

CodePudding user response:

This should work:

myList = [['Mar', 5], ['Feb', 29], ['Mar', 7], ['Jan', 20], ['Feb', 3], ['Feb', 4], ['Mar', 12], ['Jan', 2], ['Jan', 16]]
totals = {}
for i in myList:
    totals.setdefault(i[0], 0)
    totals[i[0]]  = i[1]
newList = [[*i] for i in totals.items()]
print(newList)

Output:

[['Mar', 24], ['Feb', 36], ['Jan', 38]]
  • Related