網站首頁 編程語言 正文
一、isdigit()函數
isdigit()函數是檢測輸入字符串是否只由數字組成。如果字符串只包含數字則返回 True 否則返回 False。
dream = "123456" print(dream.isdigit()) # 返回:True dream = "123abc456" print(dream.isdigit()) # 返回:False dream = 'abcd' print(dream.isdigit()) # 返回:False
二、filter() 函數
說明:filter() 函數用于過濾序列,過濾掉不符合條件的元素,返回一個迭代器對象;
如果要轉換為列表,可以使用 list() 來轉換。
該接收兩個參數,第一個為函數,第二個為序列,序列的每個元素作為參數傳遞給函數進行判斷,然后返回 True 或 False,最后將返回 True 的元素放到新列表中。
語法:
filter(function, iterable)
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)
2、過濾出列表中的所有偶數:
l = [x for x in range(10)] print(list(filter(lambda x : x%2 == 0, l)))
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)
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)))
5、去除空格和空值
def not_empty(s): return s and s.strip() filter(not_empty, ['A', '', 'B', None, 'C', ' '])
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
三、提取一段字符串中的數字
列表轉字符串
number = ['12', '333', '4'] number_ = "".join(number) # 列表轉字符串 print(number_) # 123334
a = "".join(list(filter(str.isdigit, '123ab45'))) print(a) # 返回12345 b = list(filter(str.isdigit, '123ab45')) print(b) # 返回['1', '2', '3', '4', '5']
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
利用正則表達式
import re str_ = "12今天333天氣4不錯" number = re.findall("\d+",str_) # 輸出結果為列表 print(number) # 輸出結果:['12', '333', '4']
四、匹配指定字符串開頭的數字
例如下面的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
提取 calibration=0.9863265752792358 .
# 匹配“calibration=”后面的數字 pattern = re.compile(r'(?<=calibration=)\d+\.?\d*') pattern.findall(string) # ['0.9863265752792358']
五、匹配時間,17:35:24
string = "WARNING:tensorflow: 20181011 15:28:39 Initialize training" pattern = re.compile(r'\d{2}:\d{2}:\d{2}') pattern.findall(string) # ['15:28:39']
六、匹配時間,20181011 15:28:39
string = "WARNING:tensorflow: 20181011 15:28:39 Initialize training" pattern = re.compile(r'\d{4}\d{2}\d{2}\s\d{2}:\d{2}:\d{2}') pattern.findall(string) # ['20181011 15:28:39']
總結
原文鏈接:https://blog.csdn.net/luoxuexi2020/article/details/116666167
相關推薦
- 2022-04-01 k8s報錯error: You must be logged in to the server (U
- 2024-03-21 Nacos簡介
- 2022-03-14 easypoi導入校驗跳過空行_EasyPoi導入驗證功能
- 2022-09-04 C++實現ETW進行進程變動監控詳解_C 語言
- 2022-01-03 CSS字體屬性之復合屬性
- 2022-05-21 服務發現與負載均衡機制Service實例創建_服務器其它
- 2022-02-11 Android之Compose頁面切換動畫介紹_Android
- 2022-02-22 解決:DevTools failed to load SourceMap:... net::ERR_
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支