2022年 11月 4日

python提取字符串中的数字

一、isdigit()函数

isdigit()函数是检测输入字符串是否只由数字组成。如果字符串只包含数字则返回 True 否则返回 False。

dream = "123456"
print(dream.isdigit())
# 返回:True

dream = "123abc456"
print(dream.isdigit())
# 返回:False

dream = 'abcd'
print(dream.isdigit())
# 返回:False
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

二、filter() 函数

说明:filter() 函数用于过滤序列,过滤掉不符合条件的元素,返回一个迭代器对象;

如果要转换为列表,可以使用 list() 来转换。

该接收两个参数,第一个为函数,第二个为序列,序列的每个元素作为参数传递给函数进行判断,然后返回 True 或 False,最后将返回 True 的元素放到新列表中。

语法:

filter(function, iterable)
  • 1

1、过滤出列表中的所有奇数:

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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

2、过滤出列表中的所有偶数:

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

3、过滤出1~100中平方根是整数的数:

import math
def is_sqr(x):
    return math.sqrt(x) % 1 == 0
 
tmplist = filter(is_sqr, range(1, 101))
newlist = list(tmplist)
print(newlist)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

4、删除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 n

print(list(filter(isprimer, L)))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

5、去除空格和空值

def not_empty(s):
  return s and s.strip()

filter(not_empty, ['A', '', 'B', None, 'C', ' '])
  • 1
  • 2
  • 3
  • 4

6、高阶运用

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

三、提取一段字符串中的数字

  • 列表转字符串
number = ['12', '333', '4']
number_ = "".join(number)    # 列表转字符串
print(number_)    # 123334
  • 1
  • 2
  • 3
a = "".join(list(filter(str.isdigit, '123ab45')))
print(a)
# 返回12345

b = list(filter(str.isdigit, '123ab45'))
print(b)
# 返回['1', '2', '3', '4', '5']
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
time_ = "2019年09月04日 11:00"
time_filter = filter(str.isdigit, time_)

print(time_filter)           # <filter object at 0x0000019358731BE0>
print(type(time_filter))     # <class 'filter'>
time_list = list(time_filter)       # ['2', '0', '1', '9', '0', '9', '0', '4', '1', '1', '0', '0']
time_str = "".join(time_list)       # 转为str    201909041100
time_int = int(time_str)            # 转为int    201909041100
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 利用正则表达式
import re
str_ = "12今天333天气4不错"
number = re.findall("\d+",str_)    # 输出结果为列表
print(number)
 
# 输出结果:['12', '333', '4']
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6