如何删除Lis中最大的变量

2024-05-01 22:09:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在编写一些代码,打印列表中最大的项目,然后删除最大的项目。它重复这个过程,按降序打印项目。你知道吗

名单的每一部分都包含一个志愿者的名字,他们统计的行李总数,以及他们的准确性。该算法检查精度最高的零件,然后打印上述三个变量。你知道吗

不幸的是,我不知道如何找到列表中最大部分的位置(它的索引),不允许我删除它。你知道吗

我已经尝试过使用list.index命令以及只键入del[maximum = max(VolunteerList, key = operator.itemgetter(2))],但都不起作用。你知道吗

Volunteer1TotalBags = 5
Volunteer2TotalBags = 23
Volunteer3TotalBags = 567
Volunteer4TotalBags = 12
Volunteer5TotalBags = 90
Volunteer6TotalBags = 11

Volunteer1Accuracy = 12
Volunteer2Accuracy = 65
Volunteer3Accuracy = 3
Volunteer4Accuracy = 5
Volunteer5Accuracy = 345
Volunteer6Accuracy = 43

def Option1():
    global Volunteer1Forename
    global Volunteer2Forename
    global Volunteer3Forename
    global Volunteer4Forename
    global Volunteer5Forename
    global Volunteer6Forename
    global Volunteer1Surname
    global Volunteer2Surname
    global Volunteer3Surname
    global Volunteer4Surname
    global Volunteer5Surname
    global Volunteer6Surname
    VolunteerData() # This calls back the variables above from a previous function
    Volunteer1FullName = Volunteer1Forename + Volunteer1Surname + "'s Total Bags and Accuracy"
    Volunteer2FullName = Volunteer2Forename + Volunteer2Surname + "'s Total Bags and Accuracy"
    Volunteer3FullName = Volunteer3Forename + Volunteer3Surname + "'s Total Bags and Accuracy"
    Volunteer4FullName = Volunteer4Forename + Volunteer4Surname + "'s Total Bags and Accuracy"
    Volunteer5FullName = Volunteer5Forename + Volunteer5Surname + "'s Total Bags and Accuracy"
    Volunteer6FullName = Volunteer6Forename + Volunteer6Surname + "'s Total Bags and Accuracy"
    VolunteerList = {(Volunteer1FullName, Volunteer1TotalBags, Volunteer1Accuracy), (Volunteer2FullName, Volunteer2TotalBags, Volunteer2Accuracy), (Volunteer3FullName, Volunteer3TotalBags, Volunteer3Accuracy), (Volunteer4FullName, Volunteer4TotalBags, Volunteer4Accuracy), (Volunteer5FullName, Volunteer5TotalBags, Volunteer5Accuracy), (Volunteer6FullName, Volunteer6TotalBags, Volunteer6Accuracy)}
    maximum = max(VolunteerList, key = operator.itemgetter(2))
    maximumindex = VolunteerList.index(max(VolunteerList, key = operator.itemgetter(2)))
    print(maximum)
    del[maximumindex]
Option1()

这是我得到的错误,这是因为程序无法为列表的最大部分生成索引:

maximumindex = VolunteerList.index(max(VolunteerList, key = operator.itemgetter(2)))
AttributeError: 'set' object has no attribute 'index'

如果你不明白我的困境,请告诉我,我不知道我是否说得够清楚。你知道吗


Tags: and项目key列表indexglobaloperatorbags
1条回答
网友
1楼 · 发布于 2024-05-01 22:09:58

您正在这里创建一个set。你知道吗

VolunteerList = {(Volunteer1FullName, Volunteer1TotalBags, Volunteer1Accuracy), (Volunteer2FullName, Volunteer2TotalBags, Volunteer2Accuracy), (Volunteer3FullName, Volunteer3TotalBags, Volunteer3Accuracy), (Volunteer4FullName, Volunteer4TotalBags, Volunteer4Accuracy), (Volunteer5FullName, Volunteer5TotalBags, Volunteer5Accuracy), (Volunteer6FullName, Volunteer6TotalBags, Volunteer6Accuracy)}

set不支持索引。你可能想要一个list

VolunteerList = [(Volunteer1FullName, Volunteer1TotalBags, Volunteer1Accuracy), (Volunteer2FullName, Volunteer2TotalBags, Volunteer2Accuracy), (Volunteer3FullName, Volunteer3TotalBags, Volunteer3Accuracy), (Volunteer4FullName, Volunteer4TotalBags, Volunteer4Accuracy), (Volunteer5FullName, Volunteer5TotalBags, Volunteer5Accuracy), (Volunteer6FullName, Volunteer6TotalBags, Volunteer6Accuracy)]

[替换{

相关问题 更多 >