我有一个嵌套的词典:
KEYS1 = ("A", "B", "C")
KEYS2 = ("X", "Y", "Z")
d = dict.fromkeys(KEYS1, dict.fromkeys(KEYS2, 0))
Run Code Online (Sandbox Code Playgroud)
我现在想要使用格式将其值嵌入到字符串中,例如
print("d['A']['X']={A,X:d}".format(**d))
Run Code Online (Sandbox Code Playgroud)
输出:
d['A']['X']=0
Run Code Online (Sandbox Code Playgroud)
这不起作用.有关如何正确执行此操作的任何建议?
Pad*_*ham 10
KEYS1 = ("A", "B", "C")
KEYS2 = ("X", "Y", "Z")
d = dict.fromkeys(KEYS1, dict.fromkeys(KEYS2, 0))
print("d['A']['X']={A[X]}".format(**d))
Run Code Online (Sandbox Code Playgroud)
输出:
d['A']['X']=0
Run Code Online (Sandbox Code Playgroud)
从python 3.6你将能够访问字符串中的dict使用Literal字符串插值:
In [23]: print(f"d['A']['X']={d['A']['X']}")
d['A']['X']=0
Run Code Online (Sandbox Code Playgroud)