Как удалить самую большую переменную в списке - PullRequest
0 голосов
/ 07 мая 2019

В настоящее время я работаю над кодом, который печатает самый большой элемент из списка, а затем удаляет этот самый большой элемент.Он повторяет это, печатая элементы в порядке убывания.

Каждая часть списка содержит имя добровольца, общее количество подсчитанных сумок и их точность.Алгоритм проверяет часть с наибольшей точностью, а затем печатает все три вышеупомянутые переменные.

К сожалению, я не знаю, как найти положение самой большой части списка (его индекса), а непозволяя мне удалить его.

Я уже пытался использовать команду 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'

Пожалуйста, скажите мне, если вы не понимаете мое затруднительное положение, я не знаю, ясно ли я сформулировал этодовольно.

1 Ответ

1 голос
/ 07 мая 2019

Вы создаете set здесь.

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

set s не поддерживает индексацию.Вы, вероятно, хотите list

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

Заменяет { на [

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...