我正在Python(3.5)的pop()函数上做书练习。说明是使用pop()从列表中删除元素。从下面的列表中,我想删除n1,n4,n5,n6,n7,n8,n9。下面的代码工作,但非常实用),我不明白为什么特定的索引只能工作到[5]。没有使用循环(我还没有),从列表中弹出特定元素的正确方法是什么?从列表中删除(弹出)特定元素
nameList = ['n1', 'n2', 'n3', 'n4', 'n5', 'n6', 'n7', 'n8', 'n9']
print('I can only invite two people to dinner...')
print('Sorry, but ', nameList.pop(0).title(), ' will not be invited to
dinner')
print('Sorry, but ', nameList.pop(3).title(), ' will not be invited to
dinner')
print('Sorry, but ', nameList.pop(4).title(), ' will not be invited to
dinner')
print('Sorry, but ', nameList.pop(5).title(), ' will not be invited to
dinner')
print('Sorry, but ', nameList.pop(-1).title(), ' will not be invited to
dinner')
print('Sorry, but ', nameList.pop(-1).title(), ' will not be invited to
dinner')
print('Sorry, but ', nameList.pop(-1).title(), ' will not be invited to
dinner')