【发布时间】:2020-04-10 03:19:10
【问题描述】:
所以我有一个字符串列表。我需要将字符串的第一部分转换为字典中的键,并将剩余部分视为值。例如,列表是:
['We have a nice weekend','Hope you all well']
它应该返回:
{'We':['have','a','nice','weekend'],'Hope':['you','all','well']}
我的尝试如下:
dict1 = {}
dict1 = {item[0]:item[1:] for item in List1}
return dict1
但这给了我一个结果:
{'W':'e have a nice weekend','H':'ope you all well'
如何解决?提前致谢。
【问题讨论】:
标签: python python-3.x string list dictionary