hk6766 发表于 2020-9-12 15:46:41

第一次自己构思编写如何用list.remove()循环清除列表内的重复内容

liebiao = ['x1','x1','x2','x5','x1','x3','x1','x1']
tishen = liebiao
cishu = True
while cishu == True:
    liebiao.remove(tishen)
    cishu = tishen in liebiao
    if cishu == False:
      print("列表内X1已清除!")

靳子轩 发表于 2020-9-12 16:24:45

还可以更简单一些:
>>> liebiao = ['x1','x1','x2','x5','x1','x3','x1','x1']
>>> rm = 'x1'
>>> while (rm in liebiao):
        liebiao.remove(rm)

       
>>> liebiao
['x2', 'x5', 'x3']
>>>
还有问题随时回复

hk6766 发表于 2020-9-13 19:18:49

靳子轩 发表于 2020-9-12 16:24
还可以更简单一些:

还有问题随时回复

谢谢指导!

靳子轩 发表于 2020-9-13 19:35:44

>>> liebiao = ['x1','x1','x2','x5','x1','x3','x1','x1']
>>> while ('x1' in liebiao):
      liebiao.remove('x1')

      
>>> liebiao
['x2', 'x5', 'x3']
>>>
也是可以的
页: [1]
查看完整版本: 第一次自己构思编写如何用list.remove()循环清除列表内的重复内容