[英]remove() returns None when I try to remove a variable from a list
I am working on a part of a program that turns a statement into a question.我正在研究将陈述变成问题的程序的一部分。
When I try to remove x<\/code> it returns
None<\/code> .
当我尝试删除
x<\/code>时,它返回
None<\/code> 。
I want it to print the sentence with that item removed, what am I 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)
That's correct behaviour.这是正确的行为。 remove
removes that element and doesn't return a value (ie returns None
). remove
删除该元素并且不返回值(即返回None
)。 You can use pop
if you wish to access the removed element.如果你想访问被删除的元素,你可以使用pop
。
somelist.remove(x)
removes the first element it finds that equals x
from somelist
. somelist.remove(x)
从somelist
删除它找到的第一个等于x
的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))
You were very very nearly right.你几乎是对的。 You dont need to create a new variable for the modified list.您不需要为修改后的列表创建新变量。 You can just do:你可以这样做:
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)
Question_tk_l.remove(x)
print (Question_tk_l)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.