Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to extract numbers from a string with python

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people don't understand the knowledge points of this article "how to extract numbers from strings with python", so Xiaobian summarizes the following contents for everyone. The contents are detailed, the steps are clear, and they have certain reference value. I hope everyone can gain something after reading this article. Let's take a look at this article "how to extract numbers from strings with python".

isdigit() function

The isdigit() function checks whether the input string consists only of digits. Returns True if the string contains only numbers otherwise returns False.

dream = "123456"print(dream.isdigit())#return: Trueddream = "123abc456"print(dream.isdigit())#return: Falsedream = 'abcd'print(dream.isdigit())#return: False

Description: The filter() function is used to filter sequences, filter out elements that do not meet the conditions, and return an iterator object;

If you want to convert to a list, you can use list() to convert.

This takes two arguments, the first a function, the second a sequence, each element of the sequence is passed as an argument to the function for judgment, then returns True or False, and finally puts the elements that return True into a new list.

Grammar:

filter(function, iterable)

Filter out all odd numbers in the list:

def is_odd(n): return n % 2 == 1 tmplist = filter(is_odd, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])newlist = list(tmplist)print(newlist)

Filter out all even numbers in the list:

l = [x for x in range(10)]print(list(filter(lambda x : x%2 == 0, l)))

3. Filter out the numbers whose square roots are integers from 1 to 100:

import mathdef is_sqr(x): return math.sqrt(x) % 1 == 0 tmplist = filter(is_sqr, range(1, 101))newlist = list(tmplist)print(newlist)

4. Delete primes from 1-100

L = range(1, 101)def isprimer(n): flag = 1 for i in range(2, n): if n % i == 0: flag = 0 if flag == 0: return nprint(list(filter(isprimer, L)))

5. Remove spaces and nulls

def not_empty(s): return s and s.strip()filter(not_empty, ['A', '', 'B', None, 'C', ' '])

6, high-level use

def _odd_iter(): n = 1 while True: n = n + 2 yield n def _not_divisible(n): return lambda x : x%n>0 def primes(): yield 2 it = _odd_iter() ftr = filter(_not_divisible(2), it) #1 while True: n = next(ftr ) #2 yield n ftr = filter(_not_divisible(n), ftr ) #3 for n in primes(): if n < 100: print('now:',n) else: Break three, extract a number in a string

List to String

number = ['12', '333', '4']number_ = "".join(number) #list to string print(number_) # 123334a = "".join(list(filter(str.isdigit, '123ab 45')))print(a)#returns 12345b = list(filter(str.isdigit, '123ab 45'))print(b)#returns ['1',' 2','3',' 4','5'] time_ = "04 Sep 2019 11:00"time_filter = filter(str.isdigit, time_)print(time_filter) # print(type(time_filter)) # time_list = list(time_filter) # ['2', '0', '1', '9', '0', '9', '0', '4', '1', '1', '0', '0']time_str = "".join(time_list) #to str 201909041100time_int = int(time_str) #to int 201909041100

Using regular expressions

import restr_ = "12 Today 333 Weather 4 Good"number = re.findall("\d+",str_) #Output result is a list print(number) #Output result: ['12',' 333','4']

For example, the following string:

tensorflow:Final best valid 0 loss=0.20478513836860657 norm_loss=0.767241849151384 roc=0.8262403011322021 pr=0.39401692152023315 calibration=0.9863265752792358 rate=0.0

Extraction calibration=0.9863265752792358 .

#Match the digits after "calibration=" pattern = re.compile(r'(?

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report