我的对象如下所示:
{
"title": [
"The UN chief calls on the Security Council to take action in halting Yemen's slide toward anarchy.",
"Nabeel Rajab says Bahraini people are paying a heavy price for democratic and human rights values.",
"Russia and Venezuela plan to hold joint military drills in the Caribbean Sea, a source says.",
"Egypt\u2019s top prosecutor imposes a media gag order to block press coverage on recent killing of a female protester by police.",
"Multinational companies are jockeying for position as nuclear talks between Iran and international negotiators shift to high gear amid hopes of a final deal.",
"The UN refugee agency says EU maritime patrols in the Mediterranean are inadequate.",
"The US and its allies are using Ukraine to isolate Russia in order to establish a so-called one world economic and political system, an analyst says.",
"Sri Lanka\u2019s finance minister says the country will seek a bail-out of more than USD 4 billion from the IMF.",
],
"link": [
"/Detail/2015/02/12/397336/Yemen-collapsing-before-our-eyes",
"/Detail/2015/02/12/397337/Bahrainis-pay-price-for-democracy",
"/Detail/2015/02/12/397301/Russia-Venezuela-to-hold-joint-drills",
"/Detail/2015/02/12/397334/Egypt-muffling-media-on-police-abuse",
"/Detail/2015/02/12/397333/Multinational-companies-are-jockeying-for-position-as-nuclear-talks-between-Iran-and-international-negotiators-shift-to-high-gear-amid-hopes-of-a-final-deal",
"/Detail/2015/02/12/397326/UN-maritime-patrols-insufficient-UN",
"/Detail/2015/02/12/397328/US-using-Ukraine-to-isolate-Russia",
"/Detail/2015/02/12/397318/Sri-Lanka-seeks-IMF-bailout",
],
"date": [
"Thu Feb 12, 2015 6:52PM",
"Thu Feb 12, 2015 6:48PM",
"Thu Feb 12, 2015 6:40PM",
"Thu Feb 12, 2015 6:33PM",
"Thu Feb 12, 2015 6:16PM",
"Thu Feb 12, 2015 5:58PM",
"Thu Feb 12, 2015 5:53PM",
"Thu Feb 12, 2015 5:53PM",
]
}
的问题
如何将这个列表对象变成一个漂亮的字典,每个对象有1个标题,1个链接和1个日期?
我希望能够运行:
for item in items:
print(item['link'], item['title'], item['date'] )
并在每个印刷品上收到1个链接,1个标题和1个日期作为输出。我怀疑zip()可能会有所帮助,但无法弄明白。
非常感谢任何帮助
答案 0 :(得分:3)
您可以使用zip()
配对三个列表中的元素:
for link, title, date in zip(items['link'], items['title'], items['date']):
这里没有必要把任何东西变成字典。
演示:
>>> for link, title, date in zip(items['link'], items['title'], items['date']):
... print(link)
... print(title)
... print(date)
... print('------')
...
/Detail/2015/02/12/397336/Yemen-collapsing-before-our-eyes
The UN chief calls on the Security Council to take action in halting Yemen's slide toward anarchy.
Thu Feb 12, 2015 6:52PM
------
/Detail/2015/02/12/397337/Bahrainis-pay-price-for-democracy
Nabeel Rajab says Bahraini people are paying a heavy price for democratic and human rights values.
Thu Feb 12, 2015 6:48PM
------
# etc.
您也可以为每个循环迭代重建字典:
for values in zip(*items.values()):
item = dict(zip(items.keys(), values))
print(item)
这取决于dict.keys()
和dict.values()
按匹配顺序生成结果的事实。
演示:
>>> for values in zip(*items.values()):
... item = dict(zip(items.keys(), values))
... print(item)
...
{'date': 'Thu Feb 12, 2015 6:52PM', 'link': '/Detail/2015/02/12/397336/Yemen-collapsing-before-our-eyes', 'title': "The UN chief calls on the Security Council to take action in halting Yemen's slide toward anarchy."}
{'date': 'Thu Feb 12, 2015 6:48PM', 'link': '/Detail/2015/02/12/397337/Bahrainis-pay-price-for-democracy', 'title': 'Nabeel Rajab says Bahraini people are paying a heavy price for democratic and human rights values.'}
{'date': 'Thu Feb 12, 2015 6:40PM', 'link': '/Detail/2015/02/12/397301/Russia-Venezuela-to-hold-joint-drills', 'title': 'Russia and Venezuela plan to hold joint military drills in the Caribbean Sea, a source says.'}
# etc.
答案 1 :(得分:0)
我建议选择Martijn Pieters'解决方案,但如果你坚持使用,你可以这样做:
items = []
for i in range(len(myDict["title"])):
items.append({"title":myDict["title"][i], "link":myDict["link"][i], "date":myDict["date"][i]})
答案 2 :(得分:0)
这是你的意思吗?
items = [{"title" : items["title"][i],
"link" : items["link"][i],
"date" : items["date"][i]}
for i in range(len(items["title"]))]
import pprint
pprint.pprint(items)