Python List comprehension with items() and enumerate()
I would like to implement this:
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)
Using list comprehension. This is as close as I can get:
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/69964.html