简体   繁体   English

如何将数字从字符串提取为列表中的单个元素?

[英]How can extract numbers from a string to a list as individual elements in python?

I would like to extract the numbers from the below string element of a list of n length into a list in their original form: 我想将以下n长度列表的字符串元素中的数字提取到其原始形式的列表中:

list = ['25 birds, 1 cat, 4 dogs, 101 ants']

output = [25, 1, 4, 101]

I'm quite new to regex so I've been trying with the following: 我对regex还是很陌生,所以我一直在尝试以下方法:

[regex.findall("\d", list[i]) for i in range(len(list))]

However, the output is: 但是,输出为:

output = [2, 5, 1, 4, 1, 0, 1]

Try this : 尝试这个 :

list_ = ['25 birds, 1 cat, 4 dogs, 101 ants']
import re
list(map(int, re.findall('\d+', list_[0])))

Output : 输出

[25, 1, 4, 101]

Also, avoid assigning variable names as list . 另外,避免将变量名称分配为list

You're missing a + 您错过了+

you find all should have "\\d+", not just "\\d" 您会发现所有人都应该有“ \\ d +”,而不仅仅是“ \\ d”

We don't really need to use regex to get numbers from a string. 我们实际上并不需要使用正则表达式从字符串中获取数字。

lst = ['25 birds, 1 cat, 4 dogs, 101 ants']
nums = [int(word) for item in lst for word in item.split() if word.isdigit()]
print(nums)
# [25, 1, 4, 101]

Equivalent without list comprehension: 没有列表理解的等效项:

lst = ['25 birds, 1 cat, 4 dogs, 101 ants']
nums = []
for item in lst:
    for word in item.split():
        if word.isdigit():
            nums.append(int(word))
print(nums)
# [25, 1, 4, 101]

You can use the following function to achieve this. 您可以使用以下功能来实现。 I used re.compile given that it is a bit faster than calling re functions straight out of the module, if you have really long lists. 我使用re.compile是因为它比直接在模块中调用re函数要快一些(如果您的列表很长)。

I also used yield and finditer since I do not know how long your lists will be, so this will provide some memory efficiency, given their lazy evaluation. 我还使用了yieldfinditer因为我不知道您的列表将持续多久,因此,考虑到他们的懒惰评估,这将提供一定的存储效率。

import re

def find_numbers(iterable):
    NUMBER = re.compile('\d+')
    def numbers():
        for string in iterable:
            yield from NUMBER.finditer(iterable)

    for number in numbers():
        yield int(number.group(0))

print(list(find_numbers(['25 birds, 1 cat, 4 dogs, 101 ants'])))
# [25, 1, 4, 101]

Code: 码:

import re

list_ = ['25 birds, 1 cat, 4 dogs, 101 ants']
output = list(map(int, re.findall('\d+', list_[0])))
print(output)

output: 输出:

[25, 1, 4, 101]

Explanation: 说明:

re.findall returns list of string where strings are scanned from left to right, matches are return in the order found. re.findall返回字符串列表,其中从左到右扫描字符串,以找到的顺序返回匹配项。

map applies int to each item in list of string and returns map object map将int应用于字符串列表中的每个项目,并返回map对象

list Since map object is iterator, pass it as argument to factory method for creating list list由于地图对象是迭代器,请将其作为参数传递给用于创建列表的工厂方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM