亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何隨機化傳入格式未知的字符串中的數字?

如何隨機化傳入格式未知的字符串中的數字?

慕俠2389804 2023-05-23 10:51:17
對于 NLP 項目,我需要根據訓練示例生成用于訓練目的的隨機數字字符串。數字以字符串形式出現(來自 OCR)。讓我將此處的問題陳述限制為百分比值,其中到目前為止觀察到的格式包括以下格式或指出的格式特征的任何有意義的組合:'60'       # no percentage sign, precision 0, no other characters'60.00'    # no percentage sign, precision 2, dot for digit separation'60,000'   # no percentage sign, precision 3, comma for digit separation'60.0000'  # no percentage sign, precision 4, dot for digit separation'60.00%'   # same as above, with percentage sign'60.00 %'  # same as above, with whitespace'100%'     # three digits, zero precision, percentage sign'5'        # single digit'% 60'     # percentage sign in front of the number, whitespace我的目標是在保留每個字符格式的同時隨機化數字(例外:由于數字數量不同,當 5.6 可以隨機化為 18.7 或 100.0 時,反之亦然)。百分比數值應介于 0 和 100 之間。舉幾個我需要它的例子:input  = '5'  # integer-like digitoutput = [  '7',            '18',           '100'] input  =  '100.00 %' # 2-precision float with whitespace & percentage signoutput = [  '5.38 %',            '38.05 %',           '100.00 %']  inpput =  '% 60,000' # percentage sign, whitespace, 4-precision float, comma separatoroutput = ['% 5,5348',           '% 48,7849',           '% 100,0000'] 我怎么能這樣做?解決方案可以是概念性的,也可以是代碼示例。解決方案需要反映真實數據中可能出現的格式到目前為止,我所知道的最好的方法是為我能想到的每種格式變體強制手寫 if 子句。
查看完整描述

2 回答

?
胡子哥哥

TA貢獻1825條經驗 獲得超6個贊

以下內容似乎適用于您提供的示例輸入。我們只對找到前導整數數字和后面跟有更多數字的潛在分隔符感興趣。我們實際上不需要尋找任何空格或百分號,因為無論如何我們只對替換任何給定匹配項中的數字感興趣。如果我錯過了什么,請告訴我:


import re


pattern = "\\d{1,3}((?P<separator>[,.])(?P<floating>\\d+))?"


strings = (

    "60",

    "60.00",

    "60,000",

    "60.0000",

    "60.00%",

    "60.00 %",

    "100%",

    "5",

    "% 60",

    "% 60,000"

)


def randomize(match):

    from random import uniform


    integer, floating = divmod(uniform(0, 100), 1)


    def get_chars():

        yield str(int(integer))

        if match.group("separator") is not None:

            yield match.group("separator")

            precision = len(match.group("floating"))

            yield f"{{:.{precision}f}}".format(floating)[2:]

    return "".join(get_chars())

        

    


for string in strings:

    print(re.sub(pattern, randomize, string))

輸出:


29

95.08

51,507

9.1783

0.80%

6.56 %

16%

22

% 27

% 93,174

>>> 


查看完整回答
反對 回復 2023-05-23
?
阿波羅的戰車

TA貢獻1862條經驗 獲得超6個贊

可以調用以下函數來生成您的情況所需的隨機數。您可以進一步修改它以最適合您的情況。


import numpy as np

def random_gen():

    precison = np.random.randint(0,6)

    val = np.random.uniform(0, 100)

    val = round(val,int(precison))

    val = str(val)

    

    white_space = np.random.randint(0,3)

    rand_index = np.random.randint(0,len(val))

    val = val[0:rand_index] + ' '*white_space + val[rand_index:]

    

    if np.random.randint(0,2) > 0:

        if np.random.randint(0,2) > 0:

            val = val + "%"

        else:

            val = "%" + val

    return val


random_gen()      


查看完整回答
反對 回復 2023-05-23
  • 2 回答
  • 0 關注
  • 156 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號