我正在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')