python3 .remove returns None when i try to remove a variable from a list

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-05 05:52:45

问题


I am working on a part of a program that turns a statement into a question. When i try to remove x it returns none i want it to print the sentence with that item removed, what is it i'm doing wrong?

def Ask(Question):
        Auxiliary = ("will","might","would","do","were","are","did")
        for x in Auxiliary:
            if x in Question:
                Question_l = Question.lower()
                Question_tk_l = word_tokenize(Question)
                Aux_Rem = Question_tk_l.remove(x)
                print (Aux_Rem)

example for behaviour wanted:

"what we are doing in the woods"

should become

"what we doing in the woods"

I want to remove any auxiliary from Question.


回答1:


That's correct behaviour. remove removes that element and doesn't return a value (i.e. returns None). You can use pop if you wish to access the removed element.




回答2:


somelist.remove(x) removes the first element it finds that equals x from somelist. It doesn't return the modified list. To print the modified list, simply print the list.

print(Question_tk_l)

If you want to turn it into a nice string, you should join it with space.

print(' '.join(Question_tk_l))


来源:https://stackoverflow.com/questions/45046415/python3-remove-returns-none-when-i-try-to-remove-a-variable-from-a-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!