开发者社区> 问答> 正文

用于删除列表的Python代码不起作用

如果以下元素与第一个列表重叠,则以下函数应删除列表中的元素。但是,它仅适用于第一个示例(a和b1)。与其他人甚至不发送错误消息,我也不知道问题出在哪里。有人可以指出我正确的方向吗?

      ```js

def funct(firstone, secondone ): counter = 0 while secondone != [] and counter < len(firstone): if firstone [counter] in secondone : del(secondone[ secondone .index(firstone[counter ])]) counter += 1 return secondone a = [0, 1, 2] b1 = [1, 2, 0] b2 = [-1, 1, 1] b3 = [0,0,2] print(funct(a, b1)) print(funct(a, b2)) print(funct(b3, a))

展开
收起
几许相思几点泪 2019-12-29 19:31:38 885 0
1 条回答
写回答
取消 提交回答
  • 当条件为时,您需要继续for循环,False否则您将始终在第一次迭代中返回

     ```js
    

    while secondone != [] and counter < len(firstone): if firstone[counter] in secondone : del(secondone[secondone.index(firstone[counter])]) counter += 1 else: continue

              return secondone
    2019-12-29 19:31:51
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载