在删除字符串列表的特定值时遇到困难

发布于 2025-02-01 16:03:30 字数 537 浏览 3 评论 0原文

因此,我要做的是删除字符串列表的一些特定值,我试图使用Regex to sucess,我想删除所有没有十进制房屋的值,我不能从一个某些索引,因为INT数字的位置有所不同。

的一个

['178', '34,79', '135925,82', '135926,82']
['102021', '191', '135067,69', '858,13', '859,13']
['176054330', '12', '506', '858,13', '0,00', '1,00']

这是我试图做这样的

re.compile(r'\d{1,5},\d{2}.*')
vend_values = list(filter(r.match, vend_name))

列表

['34,79', '135925,82', '135926,82']
['135067,69', '858,13', '859,13']
['858,13', '0,00', '1,00']

示例

So, what Iam triyng to do is remove some specific values of a list of strings, I've tried to use regex to no sucess, i want to remove all values that doesnt have a decimal house, i cant just get the numbers from a certain index because the position of the int number varies.

Here's an example of the list

['178', '34,79', '135925,82', '135926,82']
['102021', '191', '135067,69', '858,13', '859,13']
['176054330', '12', '506', '858,13', '0,00', '1,00']

I've tried to do something like this:

re.compile(r'\d{1,5},\d{2}.*')
vend_values = list(filter(r.match, vend_name))

To get this result:

['34,79', '135925,82', '135926,82']
['135067,69', '858,13', '859,13']
['858,13', '0,00', '1,00']

But with that it ends up removing some lines as well

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

水中月 2025-02-08 16:03:30

您不需要使用像Regex这样的花哨的东西。在Python中,关键字中的将为您完成工作。您也可以使用列表理解:

my_list = ['102021', '191', '135067,69', '858,13', '859,13']
my_filtered_list = [s for s in my_list if ',' in s]

如果要将其转换为功能:

def list_sorter(input_list):
    return [s for s in input_list if ',' in s]

希望有帮助!

You don't need to use something as fancy as regex. In python, the in keyword will do the work for you. You can use list comprehension too:

my_list = ['102021', '191', '135067,69', '858,13', '859,13']
my_filtered_list = [s for s in my_list if ',' in s]

if you want to turn this into a function:

def list_sorter(input_list):
    return [s for s in input_list if ',' in s]

Hope that helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文