将array([[number]])转换为'number'

时间:2019-08-18 23:48:06

标签: python numpy python-3.7.4

我在python中有以下代码行:

Rad[i] = {'j': JY[:, :, 0], 'h': JY[:, :, 0] + 1j * JY[:, :,2]}

Rad[i]的输出为:

{'j': array([[0.04816103]]), 'h': array([[0.04816103-5.16810074j]])}

所以,我想将输出显示为:

{'j': '0.04816103', 'h': '0.04816103-5.16810074j'}

我该怎么办?

感谢所有人

3 个答案:

答案 0 :(得分:2)

In [24]: x=np.array([[0.04816103]])                                                                          
In [25]: x                                                                                                   
Out[25]: array([[0.04816103]])
In [26]: x.shape                                                                                             
Out[26]: (1, 1)
In [27]: x[0,0]                                                                                              
Out[27]: 0.04816103
In [28]: x[0][0]                                                                                             
Out[28]: 0.04816103
In [29]: x.squeeze()                                                                                         
Out[29]: array(0.04816103)
In [30]: _.shape                                                                                             
Out[30]: ()
In [34]: x.item()                                                                                            
Out[34]: 0.04816103

答案 1 :(得分:0)

我用作此指令,使两个参数相互引用,如下所示:

Rad[i] = {'j': str(JY[:, :, 0][0][0]), 'h': str(JY[:, :, 0][0][0] + 1j * JY[:, :,2][0][0])}

输出为:

{'j': '0.04816103', 'h': '(5.21626177-5.11993971j)'}

答案 2 :(得分:0)

因为要处理的是1个元素的数组,所以可以使用

提取其值
{
    "recordType": "inventoryitem",
    "id": "2131",
    "values": {
        "itemid": "ITEMCODE",
        "displayname": "DISPLAYNAME",
        "salesdescription": "SALESDESCRIPTION",
        "type": [
            {
                "value": "InvtPart",
                "text": "Inventory Item"
            }
        ],
        "location": [],
        "locationquantityonhand": "",
        "locationreorderpoint": "0",
        "locationpreferredstocklevel": "1",
        "formulatext": "Yes",
        "formulanumeric": "1",
        "locationquantityonorder": "",
        "formulanumeric_1": "1",
        "formulatext_1": "Yes"
    }
}

在使用数组的任何地方(即JY [:,:,0],JY [:,:,2]),只需添加.item(),输出便是您想要的