第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何從字符串中提取浮點(diǎn)數(shù)

如何從字符串中提取浮點(diǎn)數(shù)

如何從字符串中提取浮點(diǎn)數(shù)我有許多類似于Current Level: 13.4 db.我只想提取浮點(diǎn)數(shù)。我說(shuō)的是浮點(diǎn),而不是小數(shù),因?yàn)樗袝r(shí)是完整的。RegEx能做到這一點(diǎn)嗎?還是有更好的方法?
查看完整描述

3 回答

?
拉風(fēng)的咖菲貓

TA貢獻(xiàn)1995條經(jīng)驗(yàn) 獲得超2個(gè)贊

如果浮點(diǎn)數(shù)總是用十進(jìn)制表示法表示,如下所示

>>> import re>>> re.findall("\d+\.\d+", "Current Level: 13.4 db.")['13.4']

可能就夠了。

一個(gè)更有力的版本是:

>>> re.findall(r"[-+]?\d*\.\d+|\d+", "Current Level: -13.2 db or 14.2 or 3")['-13.2', '14.2', '3']

如果您想驗(yàn)證用戶輸入,也可以通過(guò)直接到它來(lái)檢查浮點(diǎn)數(shù):

user_input = "Current Level: 1e100 db"for token in user_input.split():
    try:
        # if this succeeds, you have your (first) float
        print float(token), "is a float"
    except ValueError:
        print token, "is something else"# => Would print ...## Current is something else# Level: 
        is something else# 1e+100 is a float# db is something else


查看完整回答
反對(duì) 回復(fù) 2019-07-03
?
ABOUTYOU

TA貢獻(xiàn)1812條經(jīng)驗(yàn) 獲得超5個(gè)贊

您可能會(huì)嘗試這樣的方法,它涵蓋了所有的基礎(chǔ),包括在數(shù)字之后不依賴空格:

>>> import re>>> numeric_const_pattern = r"""
...     [-+]? # optional sign
...     (?:
...         (?: \d* \. \d+ ) # .1 .12 .123 etc 9.1 etc 98.1 etc
...         |
...         (?: \d+ \.? ) # 1. 12. 123. etc 1 12 123 etc
...     )
...     # followed by optional exponent part if desired
...     (?: [Ee] [+-]? \d+ ) ?
...     """>>> rx = re.compile(numeric_const_pattern, re.VERBOSE)>>> rx.findall(".1 .12 9.1 98.1 1. 12. 1 12")
['.1', '.12', '9.1', '98.1', '1.', '12.', '1', '12']>>> rx.findall("-1 +1 2e9 +2E+09 -2e-9")['-1', '+1', '2e9', '+2E+09', '-2e-9']
>>> rx.findall("current level: -2.03e+99db")['-2.03e+99']>>>

為方便復(fù)制粘貼:

numeric_const_pattern = '[-+]? (?: (?: \d* \. \d+ ) | (?: \d+ \.? ) )(?: [Ee] [+-]? \d+ ) ?'
rx = re.compile(numeric_const_pattern, re.VERBOSE)rx.findall("Some example: Jr. it. was .23 between 2.3 and 42.31 seconds")


查看完整回答
反對(duì) 回復(fù) 2019-07-03
?
GCT1015

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超4個(gè)贊

Python文檔有一個(gè)包含+/-和指數(shù)表示法的答案

scanf() Token      Regular Expression%e, %E, %f, %g     [-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?%i                
 [-+]?(0[xX][\dA-Fa-f]+|0[0-7]*|\d+)

此正則表達(dá)式不支持使用逗號(hào)作為整個(gè)部分和小數(shù)部分之間的分隔字符(3,14159)的國(guó)際格式。在這種情況下,替換所有\.帶著[.,]在上面的浮動(dòng)正則表達(dá)式中。

                        Regular ExpressionInternational float     [-+]?(\d+([.,]\d*)?|[.,]\d+)([eE][-+]?\d+)?


查看完整回答
反對(duì) 回復(fù) 2019-07-03
  • 3 回答
  • 0 關(guān)注
  • 1537 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)