Python列表理解item()和枚举()
我想实现这一点:
asset_hist = []
for key_host, val_hist_list in am_output.asset_history.items():
for index, hist_item in enumerate(val_hist_list):
row = collections.OrderedDict([("computer_name", key_host), ("id", index), ("hist_item", hist_item)])
asset_hist.append(row)
使用列表理解。 这是尽可能接近,我可以得到:
asset_hist = [collections.OrderedDict([("computer_name", key_host), ("id", index), ("hist_item", hist_item)]) for index, hist_item in enumerate((val_hist_list, key_host)[0]) for key_host, val_hist_list in am_output.asset_history.items()]
你近在咫尺:
import collections
asset_hist = [collections.OrderedDict([("computer_name", key_host), ("id", index), ("hist_item", hist_item)]) for index, hist_item in enumerate(val_hist_list) for key_host, val_hist_list in am_output.asset_history.items()]
链接地址: http://www.djcxy.com/p/69963.html