从字典值中提取元素

时间:2017-11-26 10:45:36

标签: python python-3.x

我有以下词典:

In [37]: print(paths)
{'definition': ['dirname', 'basename'],
'condition': ['isdir', 'isfile', 'islink', 'isabs', 'ismount', 'exists', 'lexists'],
'get': ['getatime', 'getctime', 'getmtime', 'getsize'],
'split': ['split', 'splitdrive', 'splitext', 'join', 'normcase'],
'expand': ['expanduser', 'expandvars'],
'same': ['samefile', 'sameopenfile', 'samestat'],
'path': ['abspath', 'commonpath', 'defpath', 'genericpath', 'normpath', 'pathsep', 'realpath', 'relpath']}

我想要的结果是path_list

In [38]: path_list = []
    ...: for v in paths.values():
    ...:     path_list.append(v)
In [42]: print(path_list)
['dirname', 'basename', 'isdir', 'isfile', 'islink', 'isabs', 'ismount', 'exists', 'lexists', 'getatime', 'getctime', 'getmtime', 'getsize', 'split', 'splitdrive', 'splitext', 'join', 'normcase', 'expanduser', 'expandvars', 'samefile', 'sameopenfile', 'samestat', 'abspath', 'commonpath', 'defpath', 'genericpath', 'normpath', 'pathsep', 'realpath', 'relpath']

我尝试使用内联代码解决问题。

In [43]: [ v for v in paths.values()]
Out[43]:
[['dirname', 'basename'],
 ['isdir', 'isfile', 'islink', 'isabs', 'ismount', 'exists', 'lexists'],
 ['getatime', 'getctime', 'getmtime', 'getsize'],
 ['split', 'splitdrive', 'splitext', 'join', 'normcase'],
 ['expanduser', 'expandvars'],
 ['samefile', 'sameopenfile', 'samestat'],
 ['abspath',
  'commonpath',
  'defpath',
  'genericpath',
  'normpath',
  'pathsep',
  'realpath',
  'relpath']]

它没有产生我的结果。

如何完成它?

0 个答案:

没有答案