简体   繁体   English

为什么list.remove(value)根本不起作用?

[英]Why is list.remove(value) not working at all?

I have this code: 我有以下代码:

def game():
    for i in range(0, 10):
        questions = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
        randomInt = choice(questions)
        print(formatIt(songs[randomInt], artists[randomInt]))
        questions.remove(randomInt)

( formatIt is one of my own functions and choice is random.choice ) formatIt是我自己的功能之一, choicerandom.choice

Everything works as it should until the questions.remove at the bottom, which does nothing (with no error message). 一切正常,直到在底部的questions.remove为止,它什么也不做(没有错误消息)。 I have tried changing the list items to strings; 我尝试将列表项更改为字符串; using a set rather than a list, but this isn't compatible with the random.choice and changing the variable to an actual value, but the list stays unchanged. 使用集合而不是列表,但这与random.choice不兼容,并将变量更改为实际值,但列表保持不变。

Two things, firstly: 首先有两件事:

questions.remove(questions[randint(0,9)])   # otherwise it can try removing like 65 or something

Secondly: I don't know if it's intended, but in every step inside loop you are bringing questions back to its original state. 其次:我不知道它是否是预期的,但是在循环的每一步中,您都将问题带回到其原始状态。 If you dont want that, put this line before for: 如果您不希望这样做,请将此行放在以下位置:

def game():
    questions = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    for i in range(0, 10):
        # rest of code

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM