鑑於多個列表:組合多個列表中的元素?
>>> foo = [hex, oct, abs, round, divmod, pow]
>>> bar = [format, ord, chr, ascii, bin]
and others
我與幾個嵌套的條件
每個元素具有嵌套條件完成它1.retrieve可變從系統
>>> dir()
['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'bar', 'foo']
>>> [e for e in dir() if '__' not in e]
['bar', 'foo']
>>> mul_list = [e for e in dir() if '__' not in e]
>>> mul_list
['bar', 'foo']
2.obtain
>>> [ e.__name__ for single_list in mul_list for e in eval(single_list)]
['format', 'ord', 'chr', 'ascii', 'bin', 'hex', 'oct', 'abs', 'round', 'divmod', 'pow']
如何用簡單的代碼提取e legantly?
您是否嘗試運行此代碼,因爲我在運行時遇到了錯誤 – Kallz
@Kallz如果'__'不在e]中,您需要在dir()中執行mul_list = [e for e]。 –
檢查我的答案,給我錯誤 – Kallz