Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中将字符串列表转换为字典_Python_List_Dictionary_Web Scraping - Fatal编程技术网

如何在python中将字符串列表转换为字典

如何在python中将字符串列表转换为字典,python,list,dictionary,web-scraping,Python,List,Dictionary,Web Scraping,我有一个字符串列表。我想转换成字典。我在抓取数据后得到了输出 ['Name:Dr. Mak', 'Location: India, Delhi'] ['Name:Dr. Hus MD', 'Location:US, NY'] 我想输出如下 {'Name':'Dr. Mak', 'Location': 'India, Delhi'} {'Name':'Dr. Hus MD', 'Location':'US, NY'} 编辑:删除空白 dict(map(str.strip, s.split('

我有一个字符串列表。我想转换成字典。我在抓取数据后得到了输出

['Name:Dr. Mak', 'Location: India, Delhi']
['Name:Dr. Hus MD', 'Location:US, NY']
我想输出如下

{'Name':'Dr. Mak', 'Location': 'India, Delhi'}
{'Name':'Dr. Hus MD', 'Location':'US, NY'}

编辑:删除空白

dict(map(str.strip, s.split(':')) for s in list_of_strings)

您尝试了什么?谢谢,它起作用了。我认为这个好的解决方案仍然缺少应用于dict值的str.strip()函数。例如dict([s.split(':')[0],s.split(':')[1]。strip()]对于字符串列表中的s)如果
s
中有多个冒号,那么
s.split(':',1)
会更安全。@taras True,但我不明白为什么在
国家、城市中会有冒号schema@FHTMitchell)让我们称之为未来的证明。
dict(map(str.strip, s.split(':')) for s in list_of_strings)