假设有一个宠物列表,其中包含多个值为“cat”的元素。要删除所有这些元素,可不断运行一个while循环。编辑pets.py文件:
pets = ['dog','cat','dog','goldfish','cat','rabbit','cat']
print(pets)
while 'cat' in pets:
pets.remove('cat')
print(pets)
运行结果如下:
[root@centos7 python]# python36 pets.py
['dog', 'cat', 'dog', 'goldfish', 'cat', 'rabbit', 'cat']
['dog', 'dog', 'goldfish', 'rabbit']
---------------------
【转载】仅作分享,侵删
作者:zsx0728
原文:https://blog.csdn.net/zsx0728/article/details/80607430
|
|