Tôi đang nhận dữ liệu dưới dạng từ điển từ cơ sở dữ liệu như:
Nội dung chính
- Làm thế nào để bạn thay thế một giá trị trong một chức năng python?
- Làm thế nào để bạn thay thế các giá trị?
- Thay thế [] có nghĩa là gì trong python?
- Làm thế nào để bạn thay thế một số bằng một số khác trong Python?
in_dict = {"x":2,"y":"x+2","z":"y+2"}
Và tôi đang nhận phương trình toán học của mình dưới dạng chuỗi như:
eqn = "[x+2]*y/z"
Phương pháp dễ nhất để thay thế các giá trị trong phương trình và đưa ra giải pháp cuối cùng là gì?
Lưu ý: Người ta nên biết rằng các giá trị trong từ điển đến ngẫu nhiên.
Đã hỏi ngày 29 tháng 12 năm 2017 lúc 5:43Dec 29, 2017 at 5:43Dec 29, 2017 at 5:43
4
Xây dựng câu trả lời
eqn = "[x+2]*y/z"
4 trước đây, đây là một cách tiếp cận năng động đối với kích thước và nội dung của các biến eqn = "[x+2]*y/z"
5. Bước lớn nhất là ánh xạ các ký hiệu Sympy [Python Vars của eqn = "[x+2]*y/z"
6] đến các biểu tượng tượng trưng của chúng được truyền qua eqn = "[x+2]*y/z"
5 của bạnimport sympy
def eval_eqn[eqn,in_dict]:
subs = {sympy.symbols[key]:item for key,item in in_dict.items[]}
ans = sympy.simplify[eqn].evalf[subs = subs]
return ans
Các đầu vào ban đầu:
in_dict = {"x":2,"y":"x+2","z":"y+2"}
eqn = "[x+2]*y/z"
print[eval_eqn[eqn,in_dict]]
eqn = "[x+2]*y/z"
8Một tập hợp đầu vào khác:
in_dict = {"a":2, "b": 4, "c":"a+2*b","d":"4*b + c"}
eqn = "c**2 + d**2"
print[eval_eqn[eqn,in_dict]]
eqn = "[x+2]*y/z"
9Đã trả lời ngày 29 tháng 12 năm 2017 lúc 17:32Dec 29, 2017 at 17:32Dec 29, 2017 at 17:32
1
Giả sử bạn chỉ có ba biến,
eqn = "[x+2]*y/z"
1Bạn có thể sử dụng Eval:
eqn = "[x+2]*y/z"
2[Chỉnh sửa 2] Bạn có thể sử dụng ItemGetter để giải nén:
eqn = "[x+2]*y/z"
3Đã trả lời ngày 29 tháng 12 năm 2017 lúc 5:50Dec 29, 2017 at 5:50Dec 29, 2017 at 5:50
1
Bạn có thể sử dụng Sympy để giải các phương trình này. Cài đặt nó nếu bạn chưa sử dụng nó bằng cách sử dụng
import sympy
def eval_eqn[eqn,in_dict]:
subs = {sympy.symbols[key]:item for key,item in in_dict.items[]}
ans = sympy.simplify[eqn].evalf[subs = subs]
return ans
0eqn = "[x+2]*y/z"
5Điều này sẽ cung cấp cho bạn đầu ra như:
eqn = "[x+2]*y/z"
8Đã trả lời ngày 29 tháng 12 năm 2017 lúc 6:04Dec 29, 2017 at 6:04Dec 29, 2017 at 6:04
Sohaib Farooqisohaib FarooqiSohaib FarooqiSohaib Farooqi
5.1473 Huy hiệu vàng29 Huy hiệu bạc41 Huy hiệu đồng3 gold badges29 silver badges41 bronze badges3 gold badges29 silver badges41 bronze badges
Mã mẫu:
eqn = "[x+2]*y/z"
7Đầu ra mẫu:
eqn = "[x+2]*y/z"
0Đã trả lời ngày 29 tháng 12 năm 2017 lúc 5:55Dec 29, 2017 at 5:55Dec 29, 2017 at 5:55
SharadsharadSharadSharad
7.8773 huy hiệu vàng18 Huy hiệu bạc35 Huy hiệu đồng3 gold badges18 silver badges35 bronze badges3 gold badges18 silver badges35 bronze badges
2
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọcsympy.subs[] method, we can substitute all instances of a variable or expression in a mathematical expression with some other variable or expression or value.
Bàn luận math_expression.subs[variable, substitute]
Đọcsympy.subs[] method, we can substitute all instances of a variable or expression in a mathematical expression with some other variable or expression or value.
variable – It is the variable or expression which will be substituted.
substitute – It is the variable or expression or value which comes as substitute.Bàn luận math_expression.subs[variable, substitute] Returns the expression after the substitution.
Với sự trợ giúp của phương thức sympy.sub [], chúng ta có thể thay thế tất cả các trường hợp của một biến hoặc biểu thức trong một biểu thức toán học với một số biến hoặc biểu thức hoặc giá trị khác.variable – It is the variable or expression which will be substituted.substitute – It is the variable or expression or value which comes as substitute.
Cú pháp: math_expression.sub [biến, thay thế] Returns the expression after the substitution.sympy.subs[] method, we can find the resulting expression after substituting a variable or expression with some other variable or expression or value. Here we use
Các tham số: Biến - đó là biến hoặc biểu thức sẽ được thay thế.Trả về: Trả về biểu thức sau khi thay thế.sympy.subs[] method, we can find the resulting expression after substituting a variable or expression with some other variable or expression or value. Here we use
import sympy
def eval_eqn[eqn,in_dict]:
subs = {sympy.symbols[key]:item for key,item in in_dict.items[]}
ans = sympy.simplify[eqn].evalf[subs = subs]
return ans
2 method also to declare a variable as symbol.Ví dụ 1:Output:
Trong ví dụ này, chúng ta có thể thấy rằng bằng cách sử dụng phương thức sympy.sub [], chúng ta có thể tìm thấy biểu thức kết quả sau khi thay thế một biến hoặc biểu thức bằng một số biến hoặc biểu thức hoặc giá trị khác. Ở đây chúng tôi sử dụng phương phápExample #2:
in_dict = {"a":2, "b": 4, "c":"a+2*b","d":"4*b + c"}
eqn = "c**2 + d**2"
print[eval_eqn[eqn,in_dict]]
6import sympy
def eval_eqn[eqn,in_dict]:
subs = {sympy.symbols[key]:item for key,item in in_dict.items[]}
ans = sympy.simplify[eqn].evalf[subs = subs]
return ans
8 in_dict = {"a":2, "b": 4, "c":"a+2*b","d":"4*b + c"}
eqn = "c**2 + d**2"
print[eval_eqn[eqn,in_dict]]
8sympy.subs[] returns the solution of the resulting expression.import sympy
def eval_eqn[eqn,in_dict]:
subs = {sympy.symbols[key]:item for key,item in in_dict.items[]}
ans = sympy.simplify[eqn].evalf[subs = subs]
return ans
3 import sympy
def eval_eqn[eqn,in_dict]:
subs = {sympy.symbols[key]:item for key,item in in_dict.items[]}
ans = sympy.simplify[eqn].evalf[subs = subs]
return ans
4import sympy
def eval_eqn[eqn,in_dict]:
subs = {sympy.symbols[key]:item for key,item in in_dict.items[]}
ans = sympy.simplify[eqn].evalf[subs = subs]
return ans
5 ________ 26 & nbsp;eqn = "[x+2]*y/z"
19import sympy
def eval_eqn[eqn,in_dict]:
subs = {sympy.symbols[key]:item for key,item in in_dict.items[]}
ans = sympy.simplify[eqn].evalf[subs = subs]
return ans
8 import sympy
def eval_eqn[eqn,in_dict]:
subs = {sympy.symbols[key]:item for key,item in in_dict.items[]}
ans = sympy.simplify[eqn].evalf[subs = subs]
return ans
9eqn = "[x+2]*y/z"
22in_dict = {"x":2,"y":"x+2","z":"y+2"}
eqn = "[x+2]*y/z"
print[eval_eqn[eqn,in_dict]]
1in_dict = {"x":2,"y":"x+2","z":"y+2"}
eqn = "[x+2]*y/z"
print[eval_eqn[eqn,in_dict]]
2import sympy
def eval_eqn[eqn,in_dict]:
subs = {sympy.symbols[key]:item for key,item in in_dict.items[]}
ans = sympy.simplify[eqn].evalf[subs = subs]
return ans
8 eqn = "[x+2]*y/z"
26in_dict = {"x":2,"y":"x+2","z":"y+2"}
eqn = "[x+2]*y/z"
print[eval_eqn[eqn,in_dict]]
8 eqn = "[x+2]*y/z"
28in_dict = {"a":2, "b": 4, "c":"a+2*b","d":"4*b + c"}
eqn = "c**2 + d**2"
print[eval_eqn[eqn,in_dict]]
0in_dict = {"a":2, "b": 4, "c":"a+2*b","d":"4*b + c"}
eqn = "c**2 + d**2"
print[eval_eqn[eqn,in_dict]]
1in_dict = {"a":2, "b": 4, "c":"a+2*b","d":"4*b + c"}
eqn = "c**2 + d**2"
print[eval_eqn[eqn,in_dict]]
2in_dict = {"a":2, "b": 4, "c":"a+2*b","d":"4*b + c"}
eqn = "c**2 + d**2"
print[eval_eqn[eqn,in_dict]]
3in_dict = {"a":2, "b": 4, "c":"a+2*b","d":"4*b + c"}
eqn = "c**2 + d**2"
print[eval_eqn[eqn,in_dict]]
4in_dict = {"a":2, "b": 4, "c":"a+2*b","d":"4*b + c"}
eqn = "c**2 + d**2"
print[eval_eqn[eqn,in_dict]]
5in_dict = {"a":2, "b": 4, "c":"a+2*b","d":"4*b + c"}
eqn = "c**2 + d**2"
print[eval_eqn[eqn,in_dict]]
6import sympy
def eval_eqn[eqn,in_dict]:
subs = {sympy.symbols[key]:item for key,item in in_dict.items[]}
ans = sympy.simplify[eqn].evalf[subs = subs]
return ans
8 eqn = "[x+2]*y/z"
37eqn = "[x+2]*y/z"
38eqn = "[x+2]*y/z"
39in_dict = {"a":2, "b": 4, "c":"a+2*b","d":"4*b + c"}
eqn = "c**2 + d**2"
print[eval_eqn[eqn,in_dict]]
0in_dict = {"a":2, "b": 4, "c":"a+2*b","d":"4*b + c"}
eqn = "c**2 + d**2"
print[eval_eqn[eqn,in_dict]]
1eqn = "[x+2]*y/z"
11in_dict = {"a":2, "b": 4, "c":"a+2*b","d":"4*b + c"}
eqn = "c**2 + d**2"
print[eval_eqn[eqn,in_dict]]
3in_dict = {"a":2, "b": 4, "c":"a+2*b","d":"4*b + c"}
eqn = "c**2 + d**2"
print[eval_eqn[eqn,in_dict]]
4eqn = "[x+2]*y/z"
14Output:
eqn = "[x+2]*y/z"
2
2 cũng để khai báo một biến là biểu tượng.import sympy
def eval_eqn[eqn,in_dict]:
subs = {sympy.symbols[key]:item for key,item in in_dict.items[]}
ans = sympy.simplify[eqn].evalf[subs = subs]
return ans
3 import sympy
def eval_eqn[eqn,in_dict]:
subs = {sympy.symbols[key]:item for key,item in in_dict.items[]}
ans = sympy.simplify[eqn].evalf[subs = subs]
return ans
4import sympy
def eval_eqn[eqn,in_dict]:
subs = {sympy.symbols[key]:item for key,item in in_dict.items[]}
ans = sympy.simplify[eqn].evalf[subs = subs]
return ans
5 import sympy
def eval_eqn[eqn,in_dict]:
subs = {sympy.symbols[key]:item for key,item in in_dict.items[]}
ans = sympy.simplify[eqn].evalf[subs = subs]
return ans
6import sympy
def eval_eqn[eqn,in_dict]:
subs = {sympy.symbols[key]:item for key,item in in_dict.items[]}
ans = sympy.simplify[eqn].evalf[subs = subs]
return ans
7import sympy
def eval_eqn[eqn,in_dict]:
subs = {sympy.symbols[key]:item for key,item in in_dict.items[]}
ans = sympy.simplify[eqn].evalf[subs = subs]
return ans
8 import sympy
def eval_eqn[eqn,in_dict]:
subs = {sympy.symbols[key]:item for key,item in in_dict.items[]}
ans = sympy.simplify[eqn].evalf[subs = subs]
return ans
9import sympy
def eval_eqn[eqn,in_dict]:
subs = {sympy.symbols[key]:item for key,item in in_dict.items[]}
ans = sympy.simplify[eqn].evalf[subs = subs]
return ans
0in_dict = {"x":2,"y":"x+2","z":"y+2"}
eqn = "[x+2]*y/z"
print[eval_eqn[eqn,in_dict]]
1in_dict = {"x":2,"y":"x+2","z":"y+2"}
eqn = "[x+2]*y/z"
print[eval_eqn[eqn,in_dict]]
2in_dict = {"x":2,"y":"x+2","z":"y+2"}
eqn = "[x+2]*y/z"
print[eval_eqn[eqn,in_dict]]
8 import sympy
def eval_eqn[eqn,in_dict]:
subs = {sympy.symbols[key]:item for key,item in in_dict.items[]}
ans = sympy.simplify[eqn].evalf[subs = subs]
return ans
4in_dict = {"x":2,"y":"x+2","z":"y+2"}
eqn = "[x+2]*y/z"
print[eval_eqn[eqn,in_dict]]
6import sympy
def eval_eqn[eqn,in_dict]:
subs = {sympy.symbols[key]:item for key,item in in_dict.items[]}
ans = sympy.simplify[eqn].evalf[subs = subs]
return ans
6import sympy
def eval_eqn[eqn,in_dict]:
subs = {sympy.symbols[key]:item for key,item in in_dict.items[]}
ans = sympy.simplify[eqn].evalf[subs = subs]
return ans
7 in_dict = {"x":2,"y":"x+2","z":"y+2"}
eqn = "[x+2]*y/z"
print[eval_eqn[eqn,in_dict]]
8 in_dict = {"x":2,"y":"x+2","z":"y+2"}
eqn = "[x+2]*y/z"
print[eval_eqn[eqn,in_dict]]
9in_dict = {"x":2,"y":"x+2","z":"y+2"}
eqn = "[x+2]*y/z"
print[eval_eqn[eqn,in_dict]]
0in_dict = {"a":2, "b": 4, "c":"a+2*b","d":"4*b + c"}
eqn = "c**2 + d**2"
print[eval_eqn[eqn,in_dict]]
1in_dict = {"a":2, "b": 4, "c":"a+2*b","d":"4*b + c"}
eqn = "c**2 + d**2"
print[eval_eqn[eqn,in_dict]]
11eqn = "[x+2]*y/z"
3in_dict = {"a":2, "b": 4, "c":"a+2*b","d":"4*b + c"}
eqn = "c**2 + d**2"
print[eval_eqn[eqn,in_dict]]
4in_dict = {"a":2, "b": 4, "c":"a+2*b","d":"4*b + c"}
eqn = "c**2 + d**2"
print[eval_eqn[eqn,in_dict]]
14eqn = "[x+2]*y/z"
eqn = "[x+2]*y/z"
1in_dict = {"a":2, "b": 4, "c":"a+2*b","d":"4*b + c"}
eqn = "c**2 + d**2"
print[eval_eqn[eqn,in_dict]]
0in_dict = {"a":2, "b": 4, "c":"a+2*b","d":"4*b + c"}
eqn = "c**2 + d**2"
print[eval_eqn[eqn,in_dict]]
1in_dict = {"a":2, "b": 4, "c":"a+2*b","d":"4*b + c"}
eqn = "c**2 + d**2"
print[eval_eqn[eqn,in_dict]]
2in_dict = {"a":2, "b": 4, "c":"a+2*b","d":"4*b + c"}
eqn = "c**2 + d**2"
print[eval_eqn[eqn,in_dict]]
3in_dict = {"a":2, "b": 4, "c":"a+2*b","d":"4*b + c"}
eqn = "c**2 + d**2"
print[eval_eqn[eqn,in_dict]]
4in_dict = {"a":2, "b": 4, "c":"a+2*b","d":"4*b + c"}
eqn = "c**2 + d**2"
print[eval_eqn[eqn,in_dict]]
5Example #2:in_dict = {"a":2, "b": 4, "c":"a+2*b","d":"4*b + c"}
eqn = "c**2 + d**2"
print[eval_eqn[eqn,in_dict]]
6import sympy
def eval_eqn[eqn,in_dict]:
subs = {sympy.symbols[key]:item for key,item in in_dict.items[]}
ans = sympy.simplify[eqn].evalf[subs = subs]
return ans
8 in_dict = {"a":2, "b": 4, "c":"a+2*b","d":"4*b + c"}
eqn = "c**2 + d**2"
print[eval_eqn[eqn,in_dict]]
8sympy.subs[] returns the solution of the resulting expression.import sympy
def eval_eqn[eqn,in_dict]:
subs = {sympy.symbols[key]:item for key,item in in_dict.items[]}
ans = sympy.simplify[eqn].evalf[subs = subs]
return ans
3 import sympy
def eval_eqn[eqn,in_dict]:
subs = {sympy.symbols[key]:item for key,item in in_dict.items[]}
ans = sympy.simplify[eqn].evalf[subs = subs]
return ans
4import sympy
def eval_eqn[eqn,in_dict]:
subs = {sympy.symbols[key]:item for key,item in in_dict.items[]}
ans = sympy.simplify[eqn].evalf[subs = subs]
return ans
5 ________ 26 & nbsp;eqn = "[x+2]*y/z"
19import sympy
def eval_eqn[eqn,in_dict]:
subs = {sympy.symbols[key]:item for key,item in in_dict.items[]}
ans = sympy.simplify[eqn].evalf[subs = subs]
return ans
8 import sympy
def eval_eqn[eqn,in_dict]:
subs = {sympy.symbols[key]:item for key,item in in_dict.items[]}
ans = sympy.simplify[eqn].evalf[subs = subs]
return ans
9eqn = "[x+2]*y/z"
22in_dict = {"x":2,"y":"x+2","z":"y+2"}
eqn = "[x+2]*y/z"
print[eval_eqn[eqn,in_dict]]
1in_dict = {"x":2,"y":"x+2","z":"y+2"}
eqn = "[x+2]*y/z"
print[eval_eqn[eqn,in_dict]]
2import sympy
def eval_eqn[eqn,in_dict]:
subs = {sympy.symbols[key]:item for key,item in in_dict.items[]}
ans = sympy.simplify[eqn].evalf[subs = subs]
return ans
8 eqn = "[x+2]*y/z"
26in_dict = {"x":2,"y":"x+2","z":"y+2"}
eqn = "[x+2]*y/z"
print[eval_eqn[eqn,in_dict]]
8 eqn = "[x+2]*y/z"
28in_dict = {"a":2, "b": 4, "c":"a+2*b","d":"4*b + c"}
eqn = "c**2 + d**2"
print[eval_eqn[eqn,in_dict]]
0in_dict = {"a":2, "b": 4, "c":"a+2*b","d":"4*b + c"}
eqn = "c**2 + d**2"
print[eval_eqn[eqn,in_dict]]
1in_dict = {"a":2, "b": 4, "c":"a+2*b","d":"4*b + c"}
eqn = "c**2 + d**2"
print[eval_eqn[eqn,in_dict]]
2in_dict = {"a":2, "b": 4, "c":"a+2*b","d":"4*b + c"}
eqn = "c**2 + d**2"
print[eval_eqn[eqn,in_dict]]
3in_dict = {"a":2, "b": 4, "c":"a+2*b","d":"4*b + c"}
eqn = "c**2 + d**2"
print[eval_eqn[eqn,in_dict]]
4in_dict = {"a":2, "b": 4, "c":"a+2*b","d":"4*b + c"}
eqn = "c**2 + d**2"
print[eval_eqn[eqn,in_dict]]
5in_dict = {"a":2, "b": 4, "c":"a+2*b","d":"4*b + c"}
eqn = "c**2 + d**2"
print[eval_eqn[eqn,in_dict]]
6import sympy
def eval_eqn[eqn,in_dict]:
subs = {sympy.symbols[key]:item for key,item in in_dict.items[]}
ans = sympy.simplify[eqn].evalf[subs = subs]
return ans
8 eqn = "[x+2]*y/z"
37eqn = "[x+2]*y/z"
38eqn = "[x+2]*y/z"
39in_dict = {"a":2, "b": 4, "c":"a+2*b","d":"4*b + c"}
eqn = "c**2 + d**2"
print[eval_eqn[eqn,in_dict]]
0in_dict = {"a":2, "b": 4, "c":"a+2*b","d":"4*b + c"}
eqn = "c**2 + d**2"
print[eval_eqn[eqn,in_dict]]
1eqn = "[x+2]*y/z"
11in_dict = {"a":2, "b": 4, "c":"a+2*b","d":"4*b + c"}
eqn = "c**2 + d**2"
print[eval_eqn[eqn,in_dict]]
3in_dict = {"a":2, "b": 4, "c":"a+2*b","d":"4*b + c"}
eqn = "c**2 + d**2"
print[eval_eqn[eqn,in_dict]]
4eqn = "[x+2]*y/z"
14Ví dụ #3:Output:
eqn = "[x+2]*y/z"
3Làm thế nào để bạn thay thế một giá trị trong một chức năng python?
Với sự trợ giúp của phương thức sympy.sub [], chúng ta có thể thay thế tất cả các trường hợp của một biến hoặc biểu thức trong một biểu thức toán học với một số biến hoặc biểu thức hoặc giá trị khác. Tham số: Biến - Đó là biến hoặc biểu thức sẽ được thay thế.sympy.subs[] method, we can substitute all instances of a variable or expression in a mathematical expression with some other variable or expression or value. Parameters: variable – It is the variable or expression which will be substituted.sympy. subs[] method, we can substitute all instances of a variable or expression in a mathematical expression with some other variable or expression or value. Parameters: variable – It is the variable or expression which will be substituted.
Làm thế nào để bạn thay thế các giá trị?
Làm thế nào để thay thế một giá trị thành một biểu thức đại số...
Viết lại biểu thức thay thế cho mỗi chữ cái bằng giá trị số đã cho của nó ..
Tính tổng giá trị của biểu thức.Hãy nhớ rằng bạn phải áp dụng Bidmas ..
Thay thế [] có nghĩa là gì trong python?
Phương thức thay thế [] thay thế một cụm từ được chỉ định bằng một cụm từ được chỉ định khác.Lưu ý: Tất cả các lần xuất hiện của cụm từ được chỉ định sẽ được thay thế, nếu không có gì khác được chỉ định.replaces a specified phrase with another specified phrase. Note: All occurrences of the specified phrase will be replaced, if nothing else is specified.replaces a specified phrase with another specified phrase. Note: All occurrences of the specified phrase will be replaced, if nothing else is specified.
Làm thế nào để bạn thay thế một số bằng một số khác trong Python?
Thay thế [] tham số Phương thức thay thế [] có thể mất tối đa 3 tham số: Cựu Ước - Subring cũ bạn muốn thay thế.Mới - Chất nền mới sẽ thay thế cho chuỗi con cũ.Đếm [Tùy chọn] - Số lần bạn muốn thay thế bộ con cũ bằng bộ nền mới. Parameters The replace[] method can take maximum of 3 parameters: old - old substring you want to replace. new - new substring which will replace the old substring. count [optional] - the number of times you want to replace the old substring with the new substring. Parameters The replace[] method can take maximum of 3 parameters: old - old substring you want to replace. new - new substring which will replace the old substring. count [optional] - the number of times you want to replace the old substring with the new substring.