簡體   English   中英

如何將數學運算從str類型轉換為Python計算

[英]How to convert math operations from type of str to calculate in Python

Peharps我在Python中有一個str,例如“ 8 + 3”,甚至更困難,例如“ sin(30)”或其他。 然后如何從str的類型進行轉換以進行計算。 對於eхample,如果我的str如“ 8 + 3”,則它必須像整數8 + 3 = 11那樣進行計算;如果我的str如“ sin(30)”,則它必須計算math.sin(30)。

my_string = "8+3"
my_result = eval(my_string)

我將使用regex提取數字,然后進行檢查以查看對所述數字執行的操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM