python dict vs ruby hash
PHPz
PHPz 2017-04-17 11:16:44
[Python讨论组]

ruby中的Hash用法

grades = Hash.new
grades["Dorothy Doe"] = 9

参考:http://ruby-doc.org/core-2.0/Hash.htm...

python中的dict用法

>>> a = dict(one=1, two=2, three=3)
>>> b = {'one': 1, 'two': 2, 'three': 3}
>>> c = dict(zip(['one', 'two', 'three'], [1, 2, 3]))
>>> d = dict([('two', 2), ('one', 1), ('three', 3)])
>>> e = dict({'three': 3, 'one': 1, 'two': 2})
>>> a == b == c == d == e
True

参考:http://docs.python.org/2/library/stdt...

问题

如果想像ruby那样进行这样的操作:

grades["a"] << 1
grades["b"] << 2
grades["c"] << 3
...

python怎么做?

如果用这种方式:

b = {'one': 1, 'two': 2, 'three': 3}

值是唯一的,但想要'one','two','three'的数组,那里面装很多不同的值。

PHPz
PHPz

学习是最好的投资!

全部回复(0)
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号