如何将字典中的列表转换为列表中的字典(Python)

时间:2015-07-18 13:25:35

标签: python list dictionary

我在字典中有多个列表:

stock = {
  'code': ['1942', '1942', '1942', '1942'], 
  'high': [782, 771, 764, 765],
  'date': ['2015-07-15', '2015-07-14', '2015-07-13',  '2015-07-10',],
  'close': [780, 768, 763, 753],
  'open': [767, 770, 756, 762]
}

我想将这些列表转换为列表中的字典:

converted = [
  {'code': '1942', 'high': 782, 'date': '2015-07-15', 'close': 780, 'open': 767},
  {'code': '1942', 'high': 771, 'date': '2015-07-14', 'close': 768, 'open': 770},
  {'code': '1942', 'high': 764, 'date': '2015-07-13', 'close': 763, 'open': 756},
  {'code': '1942', 'high': 765, 'date': '2015-07-12', 'close': 753, 'open': 762},
]

最终目标是使用字典输出csv文件,但我被困在这里。我不完全确定如何得到结果。如果你能给我一些建议,那就太棒了。谢谢!

4 个答案:

答案 0 :(得分:4)

只需使用zip和地图:

converted = map(lambda b: dict(zip(stock.keys(),b)), zip(*stock.values()))

答案 1 :(得分:3)

您可transpose the dictionary values使用zip() function,这些与密钥配对(再次使用zip())来制作新词典:

cols = zip(*stock.values())
converted = [dict(zip(stock, column)) for column in cols]

或,合并为一行:

converted = [dict(zip(stock, column)) for column in zip(*stock.values())]

演示:

>>> stock = {
...   'code': ['1942', '1942', '1942', '1942'], 
...   'high': [782, 771, 764, 765],
...   'date': ['2015-07-15', '2015-07-14', '2015-07-13',  '2015-07-10',],
...   'close': [780, 768, 763, 753],
...   'open': [767, 770, 756, 762]
... }
>>> [dict(zip(stock, column)) for column in zip(*stock.values())]
[{'close': 780, 'date': '2015-07-15', 'high': 782, 'code': '1942', 'open': 767}, {'close': 768, 'date': '2015-07-14', 'high': 771, 'code': '1942', 'open': 770}, {'close': 763, 'date': '2015-07-13', 'high': 764, 'code': '1942', 'open': 756}, {'close': 753, 'date': '2015-07-10', 'high': 765, 'code': '1942', 'open': 762}]
>>> from pprint import pprint
>>> pprint(_, compact=True)
[{'close': 780, 'code': '1942', 'date': '2015-07-15', 'high': 782, 'open': 767},
 {'close': 768, 'code': '1942', 'date': '2015-07-14', 'high': 771, 'open': 770},
 {'close': 763, 'code': '1942', 'date': '2015-07-13', 'high': 764, 'open': 756},
 {'close': 753, 'code': '1942', 'date': '2015-07-10', 'high': 765, 'open': 762}]

答案 2 :(得分:0)

您可以使用<?php set_time_limit(0); ini_set('display_errors', 'on'); function IRCBot() { function IRC() { $config = array( 'server' => 'irc.twitch.tv', 'port' => 6667, 'channel' => '#spiritboar', 'name' => 'bin4rybot', 'nick' => 'Bin4ryBOT', 'pass' => 'oauth:##########################' //http://twitchapps.com/tmi/ ); echo 'test'; $server = array(); $server['connect'] = fsockopen($config['server'], $config['port']); if($server['connect']) { echo 'test2'; SendData("PASS " . $config['pass'] . "\n\r"); SendData("NICK " . $config['nick'] . "\n\r"); SendData("USER " . $config['nick'] . "\n\r"); SendData("JOIN " . $config['channel'] . "\n\r"); while(!feof($server['connect'])) { echo 'test3'; } } } function SendData($cmd) { global $server; fwrite($server['connect'], $cmd, strlen($cmd)); echo "[SEND] $cmd <br>"; } IRC(); } IRCBot(); ?>

zip

答案 3 :(得分:0)

这样的事可能会有所帮助吗?

converted = [dict(x) for x in zip(*[[(k, tv) for tv in v] for k, v in stock.iteritems()])]