Python字典与数学符号

Lau*_*ura 1 python dictionary symbols

我想在Python中创建一个字典,但是使用数学符号(因为那时我想用符号绘制它).

所以,我有这样的事情:

    dict_particles = {1: "Gamma", 
              2: "e+",
             -2: "e-",
              3: "mu+", 
             -3: "mu-",
              4: "pi0",
             -4: "pi+",
                  etc...}
Run Code Online (Sandbox Code Playgroud)

例如,我想要希腊字母的符号,而不是名称"Gamma".另外我想把超级和下标放到他们身上.

可能吗?

bak*_*ble 9

是的,您可以在Python代码中使用任何Unicode符号,即使是变量名称(适用于Python 3.x,适用于Python 2.6+,请查看@ a_guest的注释).

你也可以使用unicode转义("?"= "\u03c0")


Joh*_*nes 6

你用什么来绘制它们?如果你正在使用matplotlib,你可以写乳胶:

https://matplotlib.org/users/mathtext.html

然后你的dict可能看起来像这样:

dict_particles = {1: r"\gamma", 
              2: r"\epsilon",
                  etc...}
Run Code Online (Sandbox Code Playgroud)

和matplotlib将用正确的符号替换乳胶.