Abh*_*tia 10 python string dictionary python-2.7
我有一本字典
event_types={"as":0,"ah":0,"es":0,"eh":0,"os":0,"oh":0,"cs":0,"ch":0}
Run Code Online (Sandbox Code Playgroud)
我怎么能代替a通过append,s通过see,h通过horse,e通过exp与之间的空间.
输出如下:
{"append see":0,"append horse":0,"exp horse":0....}
Run Code Online (Sandbox Code Playgroud)
the*_*eye 15
有另一个字典与你的替代品,像这样
keys = {"a": "append", "h": "horse", "e": "exp", "s": "see"}
Run Code Online (Sandbox Code Playgroud)
现在,如果您的活动看起来像这样
event_types = {"as": 0, "ah": 0, "es": 0, "eh": 0}
Run Code Online (Sandbox Code Playgroud)
简单地用字典理解来重建它,就像这样
>>> {" ".join([keys[char] for char in k]): v for k, v in event_types.items()}
{'exp see': 0, 'exp horse': 0, 'append see': 0, 'append horse': 0}
Run Code Online (Sandbox Code Playgroud)
在这里," ".join([keys[char] for char in k])迭代字符,k从keys字典中提取相应的单词并形成一个列表.然后,列表的元素与空格字符连接,以获得所需的键.
| 归档时间: |
|
| 查看次数: |
2742 次 |
| 最近记录: |