2014-01-12 4 views
29

次の機能をPython 2.7より前のバージョンのPythonと互換性を持たせるにはどうすればよいですか?Python 2.7以前のディクテーションの代わりに

gwfuncs = [reboot, flush_macs, flush_cache, new_gw, revert_gw, send_log]  
gw_func_dict = {chr(2**i): func for i, func in enumerate(gwfuncs[:8])} 

答えて

60

用途:

gw_func_dict = dict((chr(2**i), func) for i, func in enumerate(gwfuncs[:8])) 

(key, value)ペアを生成するジェネレータ式とdict()機能です。

あるいは、一般的にそれを置くために、フォームの辞書の理解:

{key_expr: value_expr for targets in iterable <additional loops or if expressions>} 

はいつも使用してPythonの< 2.7で両立させることができる。

dict((key_expr, value_expr) for targets in iterable <additional loops or if expressions>) 
関連する問題