小编jhn*_*kly的帖子

如果第一个数字和长度相同,则从列表中删除数字

假设我有一个列表,[100, 210, 250, 300, 405, 430, 500, 1850, 1875, 2120, 2150] 我想删除任何以相同数字开头且长度相同的数字.结果应该是: [100, 210, 300, 405, 500, 1850, 2120]

到目前为止我所拥有的是:

for i in installed_tx_calc:
    if (len(str(i)) == 3) and (str(i)[:1] == str(i-1)[:1]):
        installed_tx_calc.remove(i)
    elif str(i)[:2] == str(i-1)[:2]:
        installed_tx_calc.remove(i)
Run Code Online (Sandbox Code Playgroud)

我有一个列表[862, 1930, 2496]和我的代码输出[1930].

搜索时我找不到任何东西,但我觉得我错过了一些明显的东西.

感谢您的时间.

python list-comprehension python-3.x

4
推荐指数
1
解决办法
68
查看次数

标签 统计

list-comprehension ×1

python ×1

python-3.x ×1