You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python字典处理函数Bug求助:排序丢数据、新增重复打印

问题分析与修复方案

现有问题

  1. generate_prioritized_list函数:按修正后的survivability(罪犯扣0.2)排序时,仅输出约一半姓名,存在无规律丢失。
  2. add_new_person函数:添加新人员后,部分姓名重复打印。

原代码

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))

问题原因与修复

1. generate_prioritized_list函数修复

原因:

  • 修正后的survivability值与原始字典中的值不匹配,通过folks[name][-1] == value匹配时无法找到对应姓名;
  • 嵌套循环会导致多个姓名匹配同一个值时重复添加,同时值不匹配的姓名直接丢失。

修复方案:
直接生成包含修正后survivability和姓名的元组列表,对该列表排序后提取姓名,确保修正后的值与姓名始终关联,避免丢失和重复。

2. add_new_person函数修复

原因:

  • 直接操作全局变量folks而非传入的unordered_people参数,逻辑依赖混乱;
  • 外部print(generate_prioritized_list(folks))会额外打印函数返回值None,同时函数内部已执行打印,导致重复输出。

修复方案:

  • 统一使用函数参数unordered_people操作字典,消除全局变量依赖;
  • 移除外部多余的print调用,仅保留函数内部的打印逻辑。

修复后的完整代码

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, 姓名)的元组列表
    prioritized = []
    for name, attrs in unordered_people.items():
        surv = attrs[-1]
        if attrs[-3]:  # 倒数第三个元素是criminal标记
            surv -= 0.2
        prioritized.append((-surv
相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 23:55:19