Home > database >  Python: sort and sum in a dictionary
Python: sort and sum in a dictionary

Time:05-05

I have a problem with this phoneBook task. First I have to sort the numbers descending and then sum all the numbers. I think that the sorting is correct but if it isnt please let me know. My biggest problem is to sum up all the numbers. The second codeblock is from my professor as help but its not really helping me. I tried several ways (with an if function, with count, and so on). Maybe I am just missing something so please help me.

This is the dictionary

phoneBook = [{'fname': 'Sophia', 'lname': 'Thorarensen', 'number': '06508641036'},
 {'fname': 'Emma', 'lname': 'Blöndal', 'number': '06504228512'},
 {'fname': 'Olivia', 'lname': 'Thorarensen', 'number': '06501126965'},
 {'fname': 'Ava', 'lname': 'Hansen', 'number': '06762858077'},
 {'fname': 'Isabella', 'lname': 'Olsen', 'number': '06801324090'},
 {'fname': 'Evelyn', 'lname': 'Bergmann', 'number': '06805592151'},
 {'fname': 'Charlotte', 'lname': 'Andersen', 'number': '06762229010'},
 {'fname': 'Liam', 'lname': 'Briem', 'number': '06767141082'},
 {'fname': 'Oliver', 'lname': 'Briem', 'number': '06802647719'},
 {'fname': 'Noah', 'lname': 'Thorlacius', 'number': '06805539375'},
 {'fname': 'William', 'lname': 'Jensen', 'number': '06765392663'},
 {'fname': 'James', 'lname': 'Hjaltalín', 'number': '06508393585'},
 {'fname': 'Oliver', 'lname': 'Scheving', 'number': '06769395459'},
 {'fname': 'Benjamin', 'lname': 'Petersen', 'number': '06505619735'},
 {'fname': 'Sophia', 'lname': 'Andersen', 'number': '06763080277'},
 {'fname': 'Charlotte', 'lname': 'Petersen', 'number': '06766619175'},
 {'fname': 'Mia', 'lname': 'Möller', 'number': '06802090547'},
 {'fname': 'Amelia', 'lname': 'Nielsen', 'number': '06765209564'},
 {'fname': 'Mia', 'lname': 'Waage', 'number': '06802348806'},
 {'fname': 'Elijah', 'lname': 'Kvaran', 'number': '06507279982'},
 {'fname': 'Logan', 'lname': 'Jensen', 'number': '06508886974'},
 {'fname': 'Logan', 'lname': 'Fjeldsted', 'number': '06763174139'},
 {'fname': 'Lucas', 'lname': 'Norddahl', 'number': '06768229218'},
 {'fname': 'Mason', 'lname': 'Berndsen', 'number': '06807930516'}]

This is my code for the descending sorting:

def sortAlg(listIn):
    total = 0
    for i in range(1, len(listIn)):
        person = listIn[i]
        j = i - 1
                
        while j >= 0 and (person['number'] > listIn[j]['number']):
            listIn[j   1] = listIn[j]
            j -= 1
        listIn[j   1] = person
        
    
    return total  # return the sum of all numbers here
    

and this is from my professor as a help

# run the algorithm on the phoneBook defined above
# the sorting algorithm is **in place**, which means the phoneBook changed after the call
t_num = sortAlg(phoneBook)
print('Sum of all numbers:', t_num)
print()
for i in phoneBook:
    print(i)
Thanks you in advance. 

CodePudding user response:

You can sort it in pythonic way like this:

phoneBook_sorted = sorted(phoneBook, reverse=True, key= lambda x: int(x['number']))

And then sum:

sum = 0
for x in phoneBook_sorted:
    sum  = int(x['number'])

CodePudding user response:

Given that the requirement is for an in place sort, you could do this:

phoneBook.sort(key=lambda e: int(e['number']))

It's worth noting that the sample data shown in the question has phone numbers as strings which are all of the same length. Given that case there's actually no need to convert to int in the lambda.

To sum the total of the phone numbers (why?):

total = sum(int(e['number']) for e in phoneBook)
  • Related