我目前正在处理一些代码,该代码可打印列表中最大的项目,然后删除该最大的项目。重复此操作,以降序打印项目。
列表的每个部分均包含志愿者姓名,他们已计算的行李总数以及其准确性。该算法检查精度最高的零件,然后打印所有上述三个变量。
不幸的是,我不知道如何找到列表最大部分(索引)的位置,不允许我删除它。
我已经尝试使用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'
请告诉我您是否不了解我的困境,我不知道我的措词是否足够清楚。
答案 0 :(得分:1)
您要在此处创建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)]
将{
替换为[