Python 官方文档:入门教程 => 点击学习
需要删除列表指定value 总结:for循环内部index(指针)每次循环自增1,删除的元素的索引会上移 lst = ['大鹏展翅','天道酬勤','前程似锦'] for el in lst: if el in ['天道酬勤
lst = ['大鹏展翅','天道酬勤','前程似锦']
for el in lst:
if el in ['天道酬勤','前程似锦']:
lst.remove(el)
print(lst)
错误结果:
['大鹏展翅', '前程似锦']
lst = ['大鹏展翅','天道酬勤','前程似锦']
del_lst =[]
for el in lst:
if el in ['天道酬勤','前程似锦']:
del_lst.append(el)
for el in del_lst:
lst.remove(el)
print(lst)
正确结果:
['大鹏展翅']
--结束END--
本文标题: python面试题
本文链接: https://www.lsjlt.com/news/183900.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0