例如,我有一个列表
mylist=[[1,2],[5,8],[-9,2],[0,-9],[4,0],[0,-4],[-1,-1]]删除-9,2的方法是
mylist.remove([-9,2])我想通过使用等于-9,2的list2的值来使用.remove(),如下所示
mylist.remove(list[2])但是它会给出一个none值,有没有其他的替代方法,因为我会循环索引>>列表值的值,每次循环都会改变。我该如何解决这个问题呢?
这就是循环
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)发布于 2018-11-09 18:12:46
您正在寻找的可能是list.pop(2),因为它删除了给定索引处的列表元素。
您也可以使用del语句,下面是它的官方文档:https://docs.python.org/3/tutorial/datastructures.html#the-del-statement
发布于 2018-11-09 18:31:08
你需要
templist=mylist.pop(p)而不是
templist=mylist.remove(mylist[p])下次,请复制粘贴完整的相关代码。
https://stackoverflow.com/questions/53223567
复制相似问题