前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >python面试题

python面试题

作者头像
py3study
发布2020-01-13 20:49:51
发布2020-01-13 20:49:51
4640
举报
文章被收录于专栏:python3
  1. 需要删除列表指定value 总结: for循环内部index(指针)每次循环自增1,删除的元素的索引会上移
代码语言:javascript
复制
lst = ['大鹏展翅','天道酬勤','前程似锦']
for el in lst:
    if el in ['天道酬勤','前程似锦']:
        lst.remove(el)
print(lst)

错误结果: ['大鹏展翅', '前程似锦']

代码语言:javascript
复制
lst = ['大鹏展翅','天道酬勤','前程似锦']
del_lst =[]
for el in lst:
    if el in ['天道酬勤','前程似锦']:
        del_lst.append(el)

for el in del_lst:
    lst.remove(el)
print(lst)

正确结果: ['大鹏展翅']

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/07/14 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档