Mathematica:使用Solve时提取数值

hbt*_*hbt 6 wolfram-mathematica

在Mathematica中,调用Solve返回一个规则列表,例如,

In[1]:= g = Solve[(x - 1) (x - 2) == 0, x]
Out[1]= {{x -> 1}, {x -> 2}}
Run Code Online (Sandbox Code Playgroud)

我怎样才能提取数值12g

我尝试使用Parteg,g[[1]]但它返回{x -> 1}而不是1.

请指教.

Sim*_*mon 10

为了补充Belisarius的答案,

x /. g
Run Code Online (Sandbox Code Playgroud)

with g = {{x -> 1}, {x -> 2}},返回列表

{1, 2}
Run Code Online (Sandbox Code Playgroud)

所以要提取第一个值1,我们可以使用

First[x /. g]
Run Code Online (Sandbox Code Playgroud)

其他替代方案是

x /. g[[1]]
(x /. g)[[1]]    (* this is equivalent to the version using First *)
g[[1,1,2]]
Run Code Online (Sandbox Code Playgroud)


Dr.*_*ius 9

x /. g[[1]]
Run Code Online (Sandbox Code Playgroud)

填充物 - >最少30个字符