python 字典相当于php的,数组中的PHP和字典在Python是一样的吗?

I have a project using python and i want to convert the php to python. I have confused in the array of php in converting it to python...

in the old code of the php... it looks like this,

array(

"Code" => 122,

"Reference" => 1311,

"Type" => 'NT',

"Amount" => 100.00

);

and this is what i did in converting it to python ...

dict = {

"Code":122,

"Reference":1311,

"Type":'NT',

"Amount":100.00

}

is my converting php to python is correct?

解决方案

Your conversion is essentially correct (though I wouldn't use dict as a variable name since that masks a built-in class constructor of the same name). That being said, PHP arrays are ordered mappings, so you should use a Python OrderedDict instead of a regular dict so that the order of insertion gets preserved:

>>> import collections

>>> od = collections.OrderedDict([

('Code', 122),

('Reference', 1311),

('Type', 'NT'),

('Amount', 100.00),

])

>>> print od['Amount']

100.0

>>> od.keys()

['Code', 'Reference', 'Type', 'Amount']

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值