如何通過字典理解來對字典進行迭代來處理它。通過理解迭代字典並獲取字典
>>> mime_types={
'.xbm': 'image/x-xbitmap',
'.dwg': 'image/vnd.dwg',
'.fst': 'image/vnd.fst',
'.tif': 'image/tiff',
'.gif': 'image/gif',
'.ras': 'image/x-cmu-raster',
'.pic': 'image/x-pict',
'.fh': 'image/x-freehand',
'.djvu':'image/vnd.djvu',
'.ppm': 'image/x-portable-pixmap',
'.fh4': 'image/x-freehand',
'.cgm': 'image/cgm',
'.xwd': 'image/x-xwindowdump',
'.g3': 'image/g3fax',
'.png': 'image/png',
'.npx': 'image/vnd.net-fpx',
'.rlc': 'image/vnd.fujixerox.edmics-rlc',
'.svgz':'image/svg+xml',
'.mmr': 'image/vnd.fujixerox.edmics-mmr',
'.psd': 'image/vnd.adobe.photoshop',
'.oti': 'application/vnd.oasis.opendocument.image-template',
'.tiff':'image/tiff',
'.wbmp':'image/vnd.wap.wbmp'
}
>>> {(key,val) for key, val in mime_types.items() if "image/tiff" == val}
這是這樣的返回結果:
set([('.tiff', 'image/tiff'), ('.tif', 'image/tiff')])
但我期待
('.tif', 'image/tiff')
我怎樣才能修改結果得到一本字典,如:
{'.tif': 'image/tiff'}
這本來是更好,如果你已經避免了'.items ()' – thefourtheye
@thefourtheye爲什麼?你不會得到'ValueError:太多值來解包'嗎? – zhangxaochen
'dict((k,mimes [k])for m in mimes if mimes [k] == image/tiff「)''''你會避免由'.items()生成的居中列表' – thefourtheye