首页 > 解决方案 > python显示列表索引必须是整数,而不是元组错误

问题描述

这是我上一篇文章中另一个用户的代码:

candidates = tempContestants[-2:]
if len(candidates) != 2:
    print("Sorry, not enough players")
    quit()
print("{} and {}, I'm sorry my dears but you are up for elimination".format(
        *candidates))
lipsync()

eliminated = random.choice(candidates)
candidates.remove(eliminated)
safe = candidates[0]
tempContestants.remove(eliminated)

print("""Ladies, I have made my decision.
{}, shantay you stay!
{}, sashay away!""".format(safe, eliminated))

if eliminated == name:
    print("You have been eliminated!")
    quit()

以下是您可能需要了解的一些信息:

season_options = {
1: ["BeBe Zahara Benet", "Nina Flowers", "Rebecca Glasscock", "Shannel", "Ongina", "Jade", "Akashia", "Tammie Brown", "Victoria Parker"],
2: ["Tyra Sanchez", "Raven", "Jujubee", "Tatianna", "Pandora Boxx", "Jessica Wild", "Sahara Davenport", "Morgan McMichaels", "Sonique", "Mystique Summers Madison", "Nicole Paige Brooks", "Shangela"],
3: ["Raja", "Manila Luzon", "Alexis Mateo", "Yara Sofia", "Carmen Carrera", "Shangela", "Delta Work", "Stacy Layne Matthews", "Mariah", "India Ferrah", "Mimi Imfurst", "Phoenix", "Venus D-Lite"],
4: ["Sharon Needles", "Chad Michaels", "Phi Phi O'Hara", "Latrice Royale", "Kenya Michaels", "Dida Ritz", "Willam", "Jiggly Caliente", "Milan", "Madame LaQueer", "The Princess", "Lashauwn Beyond", "Alisa Summers"],
5: ["Jinkx Monsoon", "Alaska", "Roxxxy Andrews", "Detox", "Coco Montrese", "Alyssa Edwards", "Ivy Winters", "Jade Jolie", "Lineysha Sparx", "Honey Mahogany", "Vivienne Pinay", "Monica Beverly Hillz", "Serena ChaCha", "Penny Tration"],
6: ["Bianca Del Rio", "Courtney Act", "Adore Delano", "Darienne Lake", "BenDeLaCreme", "Joslyn Fox", "Trinity K. Bonet", "Laganja Estranja", "Milk", "Gia Gunn", "April Carrión", "Vivacious", "Magnolia Crawford", "Kelly Mantle"],
7: ["Violet Chachki", "Ginger Minj", "Pearl", "Kennedy Davenport", "Katya", "Trixie Mattel", "Miss Fame", "Jaidynn Diore Fierce", "Max", "Kandy Ho", "Mrs. Kasha Davis", "Jasmine Masters", "Sasha Belle", "Tempest DeJour"],
8: ["Bob the Drag Queen", "Kim Chi", "Naomi Smalls", "Chi Chi DeVayne", "Derrick Barry", "Thorgy Thor", "Robbie Turner", "Acid Betty", "Naysha Lopez", "Cynthia Lee Fontaine", "Dax ExclamationPoint", "Laila McQueen"],
9: ["Sasha Velour", "Peppermint", "Shea Couleé", "Trinity Taylor", "Alexis Michelle", "Nina Bo'nina Brown", "Valentina", "Farrah Moan", "Aja", "Cynthia Lee Fontaine", "Eureka", "Charlie Hides", "Kimora Blac", "Jaymes Mansfield"],
10: ["Aquaria", "Eureka", "Kameron Michaels", "Asia O'Hara", "Miz Cracker", "Monét X Change", "The Vixen", "Monique Heart", "Blair St. Clair", "Mayhem Miller", "Dusty Ray Bottoms", "Yuhua Hamasaki", "Kalorie Karbdaashian Williams", "Vanessa Vanjie Mateo"],
11: ["Yvie Oddly", "Brooke Lynn Hytes", "A'Keria C. Davenport", "Silky Nutmeg Ganache", "Vanessa Vanjie Mateo", "Nina West", "Shuga Cain", "Plastique Tiara", "Ra'Jah O'Hara", "Scarlet Envy", "Ariel Versace", "Mercedes Iman Diamond", "Honey Davenport", "Kahanna Montrese", "Soju"],
12: ["Chad Michaels", "Raven", "Shannel", "Jujubee", "Alexis Mateo", "Yara Sofia", "Latrice Royale", "Manila Luzon", "Nina Flowers", "Tammie Brown", "Pandora Boxx", "Mimi Imfurst"],
13: ["Alaska", "Katya", "Detox", "Roxxxy Andrews", "Alyssa Edwards", "Tatianna", "Phi Phi O'Hara", "Ginger Minj", "Adore Delano", "Coco Montrese"],
14: ["Trixie Mattel", "Kennedy Davenport", "Shangela", "BeBe Zahara Benet", "Morgan McMichaels", "BenDeLaCreme", "Aja", "Chi Chi DeVayne", "Milk", "Thorgy Thor"],
15: ["Trinity the Tuck", "Monét X Change", "Monique Heart", "Naomi Smalls", "Latrice Royale", "Manila Luzon", "Valentina", "Gia Gunn", "Farrah Moan", "Jasmine Masters"],
16: ["Gothy Kendoll", "Scaredy Kat", "Vinegar Strokes", "Sum Ting Wong", "Crytal", "Blu Hydrangea", "Cheryl Hole", "Baga Chipz", "Divina De Campo", "The Vivienne"]
}

group_options = {
1: ["BeBe Zahara Benet", "Tyra Sanchez", "Raja", "Sharon Needles", "Jinkx Monsoon", "Bianca Del Rio", "Violet Chachki", "Bob the Drag Queen", "Sasha Velour", "Aquaria", "Yvie Oddly", "Chad Michaels", "Alaska", "Trixie Mattel", "Trinity the Tuck", "Monét X Change", "The Vivienne"],
2: ["Nina Flowers", "Raven", "Manila Luzon", "Chad Micheals", "Alaska", "Courtney Act", "Ginger Minj", "Kim Chi", "Peppermint", "Eureka", "Brooke Lynn Hytes", "Katya", "Kennedy Davenport", "Monique Heart", "Divina De Campo"],
3: ["Akashia", "Jujubee", "Alexis Mateo", "Latrice Royale", "Coco Montrese", "Trinity K. Bonet", "Kennedy Davenport", "Chi Chi DeVayne", "Peppermint", "Kameron Michaels", "Ra'Jah O'Hara", "Raven", "Alaska", "BenDeLaCreme", "Trinity the Tuck", "Cheryl Hole"],
4: ["Nina Flowers", "Pandora Boxx", "Yara Sofia", "Latrice Royale", "Ivy Winters", "BenDeLaCreme", "Katya", "Cynthia Lee Fontaine", "Valentina", "Monét X Change", "Nina West"],
5: ["Victoria Parker", "Shangela", "Venus D-Lite", "Alisa Summers", "Penny Tration", "Kelly Mantle", "Tempest DuJour", "Laila McQueen", "Jaymes Mansfield", "Vanessa Vanjie Mateo", "Soju", "Mimi Imfurst", "Coco Montrese", "Thorgy Thor", "Jasmine Masters", "Gothy Kendoll"],
6: ["Carmen Carrera", "Shangela", "Kenya Michaels", "Trixie Mattel", "Naysha Lopez", "Cynthia Lee Fontaine", "Eureka", "Vanessa Vanjie Mateo", "Alyssa Edwards", "Tatianna", "Morgan McMichaels", "Latrice Royale", "Manila Luzon"]
}

print('''Which group of queens do you want to compete against?
1. Winners
2. Runner-ups
3. Lip sync assassins
4. Miss Congenialities
5. First Eliminated
6. Returning queens
7. One of the seasons''')

choice = int(input())
while choice > 7 or choice < 1:
    print("Please choose one of the 7 groups")
    choice = int(input())

if choice == 7:
    print('''
Choose a season:
1. Season 1
2. Season 2
3. Season 3
4. Season 4
5. Season 5
6. Season 6
7. Season 7
8. Season 8
9. Season 9
10. Season 10
11. Season 11
12. All Stars 1
13. All Stars 2
14. All Stars 3
15. All Stars 4
16. UK Season 1''')
    choice = int(input())
    while choice > 16 or choice < 1:
        print("Please choose one of the 16 seasons")
        choice = int(input())
    contestants = season_options[choice]
else:
    contestants = group_options[choice]

tempContestants = contestants

我使用 tempContestants 以便我可以从列表中删除参赛者并添加新的参赛者,而不会影响主要参赛者列表。另外,当我想随机选择一个参赛者时,我不希望程序为两个不同的“类别”选择同一个参赛者,因此我将它们从列表中删除。

它抛出一个错误说:

bottom = tempContestants[0,len(tempContestants)-1]
    TypeError: list indices must be integers, not tuple

这是什么意思,我该如何解决?

标签: python

解决方案


线路bottom = tempContestants[0, len(tempContestants)-1]不正确。如果你想得到最后一个元素,它应该是bottom = tempContestants[-1]。如错误所示,您将 2 个值(一个元组)作为索引,这是不允许的。

如果您想获得一个新列表,则必须使用切片,即使用冒号而不是逗号:

tempContestants[:-1]  # get a copy without the last element
tempContestants[:]    # get a complete copy of the list
tempContestants[1:]   # get a copy without the first element

推荐阅读