Python 把这本字典改写成文字
我目前正试图从元组创建一个字典,但PyCharm告诉我,我的字典可以重写为字典文本。 我不知道怎么做 这是Python 2.7: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
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