简体   繁体   中英

How can I extract numbers from a string in Python, and return a list?

I am trying to find all numbers in text and return them in a list of floats.

In the text:

  • Commas are used to separate thousands
  • Several consecutive numbers are separated by a comma and a space
  • Numbers can be attached to words
text = "30feet is about 10metre but that's 1 rough estimate several numbers are like 2, 137, and 40 or something big numbers are like 2,137,040 or something"

I need to return the output as a list of floats, with commas between but no speech marks.

Eg. 
extract_numbers("1, 2, 3, un pasito pa'lante Maria")
    is [1.0, 2.0, 3.0]

Unfortunately, the output of my current attempt returns a string:


def extract_numbers(text):
  nums = re.findall(r'\b\d{1,3}(?:,\d{3})*(?:\.\d+)?(?!\d)', text)
  
    return (("[{0}]".format( 
                       ', '.join(map(str, nums))))) 

extract_numbers(TEXT_SAMPLE)

How could I return numbers within a list?

You can remove all commas from matches and then map the results to float

You can use

def extract_numbers(text):
  return [float(x.replace(',','')) for x in re.findall(r'\b\d{1,3}(?:,\d{3})*(?:\.\d+)?(?!\d)', text)]

See the Python demo :

import re
 
TEXT_SAMPLE = "30feet is about 10metre but that's 1 rough estimate several numbers are like 2, 137, and 40 or something big numbers are like 2,137,040 or something"
 
def extract_numbers(text):
  return [float(x.replace(',','')) for x in re.findall(r'\b\d{1,3}(?:,\d{3})*(?:\.\d+)?(?!\d)', text)]
 
print(extract_numbers(TEXT_SAMPLE))

# => [30.0, 10.0, 1.0, 2.0, 137.0, 40.0, 2137040.0]

This should do the trick in a clean way.

import re  
def extract_numbers(txt):
    return [float(r.replace(',', '')) for r in re.findall(r'[\d,]+', txt)]

It will first find and group all numbers and commas that are not separated and then it will return the numbers.

It's a bit of an amateur code but I guess it works

  text = """30feet is about 10metre but that's 1 rough estimate several
  numbers are like 2, 137, and 40 or something big numbers are like 2,
  137,040 or something"""
  def extract_numbers(text):
    numbers = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
    _numbers = []
    a=0
    while(a<len(text)):
      i=text[a]
      if(i in numbers):
        number=""
        while(text[a] in numbers):
          number+=text[a]
          a+=1
        _numbers.append(number)
      else:
        a+=1
    float_numbers=list()
    for i in _numbers:
      float_numbers.append(float(i))
    return float_numbers
print(extract_numbers(text))

Output: [30.0, 10.0, 1.0, 2.0, 137.0, 40.0, 2.0, 137.0, 40.0]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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