问题
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