目录

  • 常见问题和错误
    • 错误代码:
    • 测试代码:
  • 正解
    • for循环思路
    • while 循环思路
    • 简单比较for 与 while

常见问题和错误

错误代码:

def Wrong():print(f'Function runing');mm = input("please input the number you wanner delete:\n")num = list(range(98, 122))for i in num:m = iwhile m != 0:x = m% 10if x == int(mm):   #注意input的返回值是字符串类型,即使输入的是数字,也无法直接和数字比较num.remove(i)breakm = int(m/10)   #此处必须取整否则会出现小数情况,而导致%运算混乱print(num)if __name__ == "__main__":Wrong()

运行结果:

Function runing
please input the number you wanner delete:
1
[99, 101, 103, 105, 107, 109, 111, 113, 115, 117, 119, 121]Process finished with exit code 0

以上代码在执行时会由于列表发生变化,从而导致循环混乱,此处想具体观察列表变化和调试可以通过将列表调小,加入一些print语句来显示特定值或列表的方式。

测试代码:

def Wrong():print(f'Function runing');mm = input("please input the number you wanner delete:\n")num = list(range(9, 12))for i in num:m = iwhile m != 0:x = m % 10if x == int(mm):num.remove(i)breakm = int(m/10);print(num)if __name__ == "__main__":Wrong()

输出结果:

Function runing
please input the number you wanner delete:
1
[9, 11]Process finished with exit code 0

分析
使用print语句看清具体值,以及列表的变化

def Wrong():print(f'Function runing');mm = input("please input the number you wanner delete:\n")num = list(range(9, 12))for i in num:m = iprint(f'The start {num}')print(f'The start {m}')while m != 0:x = m % 10if x == int(mm):num.remove(i)breakm = int(m/10);print(f'm= {m}')print(f'The last {num}')print(num)
if __name__ == "__main__":Wrong()

结果显示:

Function runing
please input the number you wanner delete:
1
The start [9, 10, 11]
The start 9
m= 0
The last [9, 10, 11]
The start [9, 10, 11]
The start 10
m= 1
The last [9, 11]
[9, 11]Process finished with exit code 0

由此可见当删除第一个元素后,由于for 循环还保持着原先顺序,但是列表已经改变,从而导致循环无法进行,就以上代码而言,当i=10时,列表中删除之后,循环本应进行到原本10的后面的数,但10被删除后,后一个数就相当于向前移动了一位,可以把列表看做座位的集合,而for循环中i就相当于访问每一个座位,10被删除后,本来最后一个座位上的是11,但由于前一位上的数被删除了,所以自然就成了那个位上的数。当for走到11的位子上时,则会显示此位上无数,因此停止循环。

正解

for循环思路

def num_list():num = list(range(98,122))figure = [] #一个空列表,来装除删除元素之外的所有数mm = input("please input the number you wanner delete:\n")for i in num:m = iflag = 0    #作为标记作用while m != 0:x = m % 10if x != int(mm):flag += 1else:flag = 0  #一旦出现要求删除的数,马上标记清零,然后退出循环。breakm = int(m / 10)if flag != 0:   #由于前面else语句,如果是要删除的数,flag一定为0figure.append(i)print(figure)

运行结果


please input the number you wanner delete:
1
[98, 99]Process finished with exit code 0;

具体原理:
由于for循环的删除效果不行,所以采取另外一个思想,就是不对列表本身采取任何措施,把有用的取出来,放到其他地方。
如果该数符合我的条件,就会进入另外列表,若不符则留在原来列表,而题目要求删除包含某个值的数,而我采用把不包含某个值的数放入另一个列表了。

while 循环思路

def while_list():mm = input("please input the number you wanner delete:\n")num = list(range(99, 122))i = 0while i <= len(num) -1:m = num[i]print('j')   #单独一个分隔标记print(num)while m != 0:x = m % 10if x == int(mm):num.remove(num[i])i = i-1breakm = int(m / 10) #此处必须取整,否则出现小数会导致循环混乱,同时此处语句主要作用是取出其除开x的其他数字i += 1print(f'The last is {num}')

运行结果:

please input the number you wanner delete:
1
j
[99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121]
j
[99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121]
j
[99, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121]
j
[99, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121]
j
[99, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121]
j
[99, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121]
j
[99, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121]
j
[99, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121]
j
[99, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121]
j
[99, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121]
j
[99, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121]
j
[99, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121]
j
[99, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121]
j
[99, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121]
j
[99, 113, 114, 115, 116, 117, 118, 119, 120, 121]
j
[99, 114, 115, 116, 117, 118, 119, 120, 121]
j
[99, 115, 116, 117, 118, 119, 120, 121]
j
[99, 116, 117, 118, 119, 120, 121]
j
[99, 117, 118, 119, 120, 121]
j
[99, 118, 119, 120, 121]
j
[99, 119, 120, 121]
j
[99, 120, 121]
j
[99, 121]
The last is [99]

此处更能看清列表中内存变化和数字变化,当然在for 循环中也可嵌入while种处理方式。此处就不细说了,想了解的小伙伴,也可结合以上两个方式,去敲敲代码

简单比较for 与 while

while方便对列表中每一位元素的处理,而for 循环处理时,由于本身特性,相当于遍历所有元素。
平常运用中for 循环更适用于已经明确个数的列表等集合。while也可用于明确个数的列表,但更多时候用于不明个数列表,或者变化的列表。

以上均为个人见解,多有不周之处,敬请见谅

Python中,删除列表中包含某个数的所有元素相关推荐

  1. python中删除列表中的空元素以及如何读取excel中的数据

    这个暂时也没有找到更好的办法,用的是别人博客中的一种办法http://www.biofacebook.com/?p=186 while " in a: a.remove(") 其中 ...

  2. python去除excel空行_python中删除列表中的空元素以及如何读取excel中的数据

    这个暂时也没有找到更好的办法,用的是别人博客中的一种办法http://www.biofacebook.com/?p=186 while " in a: a.remove(") 其中 ...

  3. python里面列表可以同时删除吗_在python中从列表中删除项,同时对其进行迭代

    本问题已经有最佳答案,请猛点这里访问. 我正在为锦标赛应用程序编写循环算法. 当玩家数量为奇数时,我将'DELETE'添加到玩家列表中,但稍后,当我想从包含'DELETE'的日程表列表中删除所有项目时 ...

  4. python删除列表中的重复元素并保持相对顺序不变

    python删除列表中的重复元素并保持相对顺序不变 从列表中删除重复项以便所有元素都是唯一的同时保持原有相对顺序不变 对于列表我们可以使用如下方法: l1 = [1,7,7,8,5,5,4] l2 = ...

  5. python for 循环中使用 remove 删除列表中的元素

    python for 循环中使用 remove 删除列表中的元素 错误的代码 # !/usr/bin/python # encoding: utf-8 # -*- coding: utf8 -*- o ...

  6. python 删除列表中的指定元素

    python 删除列表中的指定元素 def delete_list(list1,ele):"""删除列表中的指定元素:param list1:原列表:param ele: ...

  7. Python | 程序从列表中删除范围内的所有元素

    Given a list and we have to remove elements in a range from the list in Python. 给定一个列表,我们必须从Python中的 ...

  8. python列表中怎么去掉空值_Python 如何删除列表中的空值

    今天在获取android性能CPU测试数据时,发现这么一个问题: # -*- coding:utf-8 -*- import os import time cpuInfo = os.popen(r'a ...

  9. python 如何根据索引快速删除列表中的多个元素

    一.批量删除列表中不同位置的元素 列表是python中经常用到的一种数据结构,因python提供了很多方法对其增.删.查.改,故使用起来比较灵活,下面就介绍下如何快速删除列表中多个元素的方法. 二.具 ...

  10. python处理列表中字典_Python列表嵌套字典的时候,如果要删除列表中其中一个字典要如何操作...

    如果要删除列表中其中一个字典要操作如下 第一种方法 1.1 name_list=[{"name":"张三","age":18},{" ...

最新文章

  1. 【美国斯坦福大学人工智能研究院:人工智能当以人为本】
  2. python新手自学-新手自学python
  3. 程序员需要谨记的9个安全编码规则【转载】
  4. Java-递归算法思想
  5. 01-2.C(C程序编译步骤-gcc 编译过程)
  6. Windows环境下smarty安装简明教程
  7. c++ 多重背包状态转移方程_动态规划入门——详解经典问题零一背包
  8. django-中间件
  9. 2017.10.11 灾难 失败总结
  10. iOS定位服务CoreLocation
  11. java基础-- 集合框架入门 及 List集合
  12. 使用@Validated分组遇到的坑及解决方案
  13. NLP学习01--BP神经网络
  14. 小白 C 入门并发疯学习路线(书单)
  15. 普元EOS7.x及以下版本升级Tomcat8
  16. Apple Pay编程指南(6) -沙盒测试
  17. android6.0 power按键深入分析
  18. 留一份名单,以供研究:2009中国企业500强全名单
  19. Codeforces edu round 61 D-Stressful Training 二分
  20. 使用第三方打码平台图鉴识别滑动验证码模拟登录

热门文章

  1. 基于HSV颜色空间用OpenCV-Python给照片换底
  2. 轩辕传奇服务器合并信息,合服查询-轩辕传奇官方网站-腾讯游戏-腾讯首款3D浅规则战斗网游...
  3. 苹果8官方主板价格_苹果紧急公布:部分iPhone8有严重问题,可免费换主板!
  4. 关于photoswipe的data-size问题-自适应宽高
  5. 右键快捷菜单添加WinRAR
  6. Android简单笔记本解析,成功入职头条月薪35K
  7. win7找不到服务器的dns错误怎么办,Win7百科:Win7系统DNS错误怎么办
  8. c语言 linux system,【Linux + C语言】话说,你真的了解system接口的调用吗?
  9. 微信加解密流程,证书作用讲解,官方SDK使用教程
  10. 用计算机做科学实验课件教案,大班科学实验教案3篇