如何将两个字符串转换为单个字典?

时间:2011-11-05 15:29:17

标签: python string dictionary

我正在尝试将两个字符串值转换为单个dict,如此

string1='red blue white'
string2= 'first second third'
dict={'red':first,'blue':second.'white':third}

但在这里我不能使用循环!!没有循环还有其他方法吗? 帮我!! 谢谢

1 个答案:

答案 0 :(得分:6)

>>> string1 = 'red blue white'
>>> string2 = 'first second third'
>>> dict(zip(string1.split(), string2.split()))
{'blue': 'second', 'red': 'first', 'white': 'third'}