gpt4 book ai didi

python - 从数字中过滤列表的最pythonic方法是什么?

转载 作者:行者123 更新时间:2023-11-28 20:07:59 25 4
gpt4 key购买 nike

我有这个:

['SPRD', '60', 'p25']

我想生成:

['SPRD', 'p']

最符合 Python 风格的方法是什么?

谢谢。

最佳答案

digits_stripped = (s.translate(None, '0123456789') for s in input_list)
without_blanks = [s for s in digits_stripped if s]

(请注意,如果您碰巧使用的是早于 2.6 的 Python,则需要使用 string.maketrans('', '') 而不是 None 作为 translate() 的第一个参数。)

关于python - 从数字中过滤列表的最pythonic方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15356186/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com