|
|
发表于 2018-1-4 17:39:06
|
显示全部楼层
一步一步看你就知道了
- >>> str1 = '苹果 7 桃子 6 西瓜 5 红玫瑰 4'
- >>> str2 = str1.split()
- >>> str2
- ['苹果', '7', '桃子', '6', '西瓜', '5', '红玫瑰', '4']
- >>> dict1 = {}
- >>> length = len(str2)
- >>> i = 0
- >>> j = 1
- >>> dict1[str2[i]] = str2[j]
- >>> dict1
- {'苹果': '7'}
- >>> j = 3
- >>> dict1[str2[i]] = str2[j]
- >>> dict1
- {'苹果': '6'}
- >>> j = 5
- >>> dict1[str2[i]] = str2[j]
- >>> dict1
- {'苹果': '5'}
- >>> j = 7
- >>> dict1[str2[i]] = str2[j]
- >>> dict1
- {'苹果': '4'}
- >>> i = 2
- >>> j = 1
- >>> dict1[str2[i]] = str2[j]
- >>> dict1
- {'苹果': '4', '桃子': '7'}
- >>> j = 3
- >>> dict1[str2[i]] = str2[j]
- >>> dict1
- {'苹果': '4', '桃子': '6'}
- >>>
复制代码
最后就变成那样了 |
|