如何使用列表.删除(值)/列表.pop(value)在python中使用python中的间接值

2024-04-20 01:01:05 发布

您现在位置:Python中文网/ 问答频道 /正文

例如,我有一个列表

mylist=[[1,2],[5,8],[-9,2],[0,-9],[4,0],[0,-4],[-1,-1]]

删除[-9,2]是通过

mylist.remove([-9,2])

我想通过使用list[2]的值来使用.remove(),该值等于[-9,2],如下所示

 mylist.remove(list[2])

但是它会给出一个none值,因为我正在循环索引的值>;>;list[每个循环的值变化],所以是否有其他方法可以替代它。我怎样才能解决这个问题?你知道吗

这是循环

for p in range (len(mylist)):
      if mylist[p]==startpoint:
        continue
      if mylist[p]==nextpoint:
        continue
      thirdpoint=mylist[p]
      print ('the thirdpoint is:',thirdpoint)
      if orient(startpoint[0],startpoint[1],nextpoint[0],nextpoint[1],thirdpoint[0],thirdpoint[1])>0:
        print ('ok')
        print(mylist)
        print ('the thirdpoint is:',thirdpoint)
        templist=mylist.remove(mylist[p])
        print ('templist is',templist)

        if len(templist)==2:
          print('append nextpoint:',nextpoint)
          ppoint.append(nextpoint)

Tags: thegtlenifisremovelistprint
2条回答

您要查找的可能是list.pop(2),因为它删除了给定索引处的列表元素。你知道吗

您也可以使用del语句,下面是它的官方文档:https://docs.python.org/3/tutorial/datastructures.html#the-del-statement

你需要

templist=mylist.pop(p)

而不是

templist=mylist.remove(mylist[p])

下次请复制粘贴完整的相关代码。你知道吗

相关问题 更多 >