Home > Mobile >  I'm not sure why my first function is only printing half the names in my dictionary; and why my
I'm not sure why my first function is only printing half the names in my dictionary; and why my

Time:12-05

The first function (generate_prioritized_list) is supposed to sort the names by the last number in the folks dictionary, values list (survivability)(folks[name][-1]). This function also determines if the person is a criminal (folks[name],[-3]) and if they are a criminal, 0.2 is subtracted from survivability. When I run the function I only get roughly half the names I'm supposed to get and I don't see a trend in which names disappear.

The second function is supposed to allow the user to add a person to the dictionary and then print the sorted dictionary with the new person in it. I have a bug somewhere which is causing some names to be printed twice.

folks = {'Leia': [28, 'F', 'W', False, True, 'Unemployed',1], 
        'Junipero': [15, 'M', 'E', False, False, 'Teacher', 0.21158336054026594], 
        'Sunita': [110, 'D', 'E', True, False, 'Business', 0.9834949767416051], 
        'Issur': [17, 'F', 'O', True, False, 'Service', 0.7599396397686616], 
        'Luitgard': [0, 'D', 'U', True, True, 'Unemployed', 0.8874638219100845], 
        'Rudy': [112, 'M', 'W', True, True, 'Tradesperson', 0.6035917636433216], 
        'Ioudith': [20, 'D', 'W', True, True, 'Medical', 0.24957574519928294], 
        'Helmi': [109, 'D', 'M', False, False, 'Service', 0.20239906854483214], 
        'Katerina': [108, 'M', 'W', False, True, 'Student', 0.3046268530221382], 
        'Durai': [106, 'M', 'U', True, False, 'Business', 0.32332997497778493], 
        'Euphemios': [83, 'M', 'L', True, True, 'Banker', 0.17369577419188664], 
        'Lorinda': [8, 'F', 'E', False, True, 'Retail', 0.6667783756618852], 
        'Lasse': [30, 'D', 'U', True, True, 'Business', 0.6716420300452077], 
        'Adnan': [117, 'D', 'U', True, False, 'Banker', 0.7043759366238305], 
        'Pavica': [112, 'F', 'L', False, False, 'Business', 0.5875152728319836], 
        'Adrastos': [118, 'F', 'L', False, True, 'Service', 0.0660146284846359], 
        'Kobus': [49, 'D', 'S', False, False, 'Service', 0.4738056051140088], 
        'Daniel': [115, 'D', 'L', False, True, 'Service', 0.5182765931408372], 
        'Samantha': [97, 'D', 'W', True, True, 'Medical', 0.07082409148069169], 
        'Sacagawea': [28, 'F', 'U', True, True, 'Medical', 0.29790328657890996], 
        'Ixchel': [26, 'F', 'S', False, False, 'Business', 0.22593704520870372], 
        'Nobutoshi': [31, 'M', 'W', False, True, 'Business', 0.37923896100469956], 
        'Gorou': [55, 'M', 'B', True, True, 'Banker', 0.8684653864827863], 
        'Keiko': [34, 'M', 'L', False, True, 'Student', 0.02499269016601946], 
        'Seong-Su': [1, 'M', 'M', False, True, 'Retail', 0.3214997836868769], 
        'Aya': [41, 'M', 'B', True, True, 'Teacher', 0.3378161065313626], 
        'Okan': [11, 'D', 'W', True, True, 'Banker', 0.35535128959244744], 
        'Mai': [31, 'F', 'M', False, False, 'Service', 0.7072299366468716], 
        'Chaza-el': [84, 'D', 'E', True, True, 'Teacher', 0.263795143996962], 
        'Estera': [79, 'M', 'U', True, False, 'Tradesperson', 0.09970175216521693], 
        'Dante': [82, 'M', 'L', True, False, 'Unemployed', 0.2126494288577333], 
        'Leofric': [68, 'F', 'B', True, False, 'Unemployed', 0.19591887643941486], 
        'Anabelle': [63, 'M', 'B', False, False, 'Teacher', 0.3558324357405023], 
        'Harsha': [119, 'D', 'O', False, True, 'Retail', 0.3359989642837887], 
        'Dionisia': [92, 'F', 'B', True, False, 'Doctor', 0.42704604164789706], 
        'Rajesh': [55, 'F', 'M', True, False, 'Doctor', 0.485752225148387], 
        'Scilla': [60, 'F', 'M', False, False, 'Student', 0.7294089528796434], 
        'Arsenio': [10, 'D', 'L', False, True, 'Teacher', 0.0819890866210915]}


def generate_prioritized_list(unordered_people):
    survivability = [folks[name][-1] for name in folks]
    criminal= [folks[name][-3] for name in folks]

    for i in range(len(survivability)):
        if criminal[i] == True:
            survivability[i] = survivability[i]-0.2
    
    sorted_values = sorted(survivability, reverse=True)
    sorted_names = [name for value in sorted_values
                    for name in folks
                    if folks[name][-1] == value]

    print(sorted_names)

def add_new_person(unordered_people,new_person_name,new_person_attributes):
    exists = False
    for i in folks:
        if i == new_person_name:
            exists = True
    if exists == True:
        answered = False
        print('Name already exists, would you like to replace it')
        while answered == False:
            prompt = input('yes/no: ')
            if prompt == 'yes':
                answered = True
                folks[new_person_name] = new_person_attributes
            elif prompt == 'no':
                answered = True
                print('done')
    else:
        folks[new_person_name] = new_person_attributes

    generate_prioritized_list(folks)
    
print(generate_prioritized_list(folks))

I'm expecting (generate_prioritized_list) function to output a list of names sorted by survivability-criminal

I'm expecting (add_new_person) function to allow the user to add new people to the sorted list

CodePudding user response:

Your list comprehension is comparing the person's original survivability with the value that's adjusted based on whether they're a criminal. This won't find all the criminals.

Use the key option to sorted() to call a function to get the adjusted survivability, and sort by that.

def adjust_survivability(name):
    person = folks[name]
    if person[-3]:
        return person[-1] - 0.2
    else:
        return person[-1]

sorted_names = sorted(folks, key=adjust_survivability, reverse=True)
  • Related