Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/408.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 把这本字典改写成文字_Python_Python 2.7 - Fatal编程技术网

Python 把这本字典改写成文字

Python 把这本字典改写成文字,python,python-2.7,Python,Python 2.7,我目前正试图从元组创建一个字典,但PyCharm告诉我,我的字典可以重写为字典文本。 我不知道怎么做 这是Python 2.7: tb = traceback.extract_tb(ex_traceback) my_stack = {'file': '', 'line': '', 'where': '', 'code': ''} my_stack['file'], my_stack['line'], my_stack['where'], my_stack['code'] = tb[0] prin

我目前正试图从元组创建一个字典,但PyCharm告诉我,我的字典可以重写为字典文本。 我不知道怎么做

这是Python 2.7:

tb = traceback.extract_tb(ex_traceback)
my_stack = {'file': '', 'line': '', 'where': '', 'code': ''}
my_stack['file'], my_stack['line'], my_stack['where'], my_stack['code'] = tb[0]
print my_stack
(假设
tb[0]
是4元组或类似的)

以下方法可行,尽管它不完全是dict文本:

my_stack = dict(zip(("file", "line", "where", "code"), tb[0]))
我认为PyCharm的意思是,你可以这样做:

my_stack = {
    'file': tb[0][0],
    'line': tb[0][1],
    'where': tb[0][2],
    'code': tb[0][3],
}
假设
tb[0]
是一个四元组,那么代码的第三行就相当于这个元组

如果希望所有内容都具有相同的值,则可以改为链等于:

my_stack['file'] = my_stack['line'] = my_stack['where'] = my_stack['code'] = tb[0]

将光标放在给定的行上,点击
Alt+Enter
,然后选择
Replace dictionary creation
查看结果:

tb = traceback.extract_tb(ex_traceback)
my_stack = {'file': tb[0][0], 'line': tb[0][1], 'where': tb[0][2], 'code': tb[0][3]}
print my_stack