无法更改 numpy 数组中的值

Can't change value in numpy array

我有一些代码,通过查看它应该可以完美运行。

def upc_dict_to_pandas_dataframe(upc_dict):
    #This could be done in fewer lines but I split them for debugging purposes
    d = upc_dict.items()
    d = list(d)
    d = [list(i) for i in d]

    for i in range(len(d)):
        d[i] = np.array(d[i], dtype=object)
        d[i] = np.hstack(d[i])
        x = int(d[i][3])
        d[i][3] = x

最后一行,d[i][3] = x 并没有把x赋值给d[i][3]。它的原始类型是一个 numpy 字符串,我试图用它的整数形式替换它。但是,它似乎只是完全跳过了分配行。我什至在调试模式下尝试过。我看着它将字符串数字转换为整数。但是 d[i][3].

没有任何变化

为什么会这样,我该如何解决?

提前致谢。

编辑

这里是d之后d的值=[list(i) for i in d],

<class 'list'>: [['B01A8L6KKO', ['873124006834', 'HORI Premium Protector - Pikachu Edition for Nintendo New 2DS XL', 4408]], ['B00L59D9HG', ['045496891503', 'Nintendo 3DS AC Adapter', 148]], ['B00ND0EBP4', ['873124005110', 'HORI Retro Zelda Hard Pouch for Nintendo 3DS XL - Zelda Version Edition', 4403]], ['B01MSHC8WT', ['859986005680', 'Tend Insights John Deere 100 Indoor Wi-Fi Camera', 16007]], ['B07CFLK37X', ['859986005291', 'Lynx Indoor/Outdoor Pro HD Wifi Camera', -1]], ['B076ZWVR2R', ['859986005376', 'Lynx Solar Weatherproof Outdoor WiFi Surveillance Camera with Solar Panel, Facial Recognition, Night Vision, White', 23570]], ['B0716ZNTKS', ['859986005857', 'Tend Insights Minion Cam HD Wi-Fi Camera (Despicable Me 3', 17726]], ['B00MOVY01I', ['853376004284', 'Rocksteady XS Extra Battery and Charger', -1]]]
 _len_ = 8

我想出了一个快速修复方法:

更改此行:

d[i] = np.hstack(d[i])

对此:

d[i] = list(np.hstack(d[i]))

我认为问题是 numpy 特有的。我仍然很好奇它不适用于 numpy 的原因。

为了能够调用似乎是嵌套列表的 d[i][3] 并使用 hstack,您需要 d[i] 列表位于嵌套列表中。您可以在 numpy hstack.

阅读更多内容

因此list(np.hstack(d[i]))将数组转换为嵌套列表形式。 你可以自己执行一个简单的脚本,可以看到 np.array() 实际上并不是 return 嵌套列表形式,因为它本身已经是数组形式

import numpy as np

a = np.array([1,2,3])
print(np.array(a))

# outputs [1,2,3]

随着 d 您添加了:

In [28]: d[0]                                                                                                
Out[28]: 
['B01A8L6KKO',
 ['873124006834',
  'HORI Premium Protector - Pikachu Edition for Nintendo New 2DS XL',
  4408]]
In [29]: np.array(d[0], object)                                                                              
Out[29]: 
array(['B01A8L6KKO',
       list(['873124006834', 'HORI Premium Protector - Pikachu Edition for Nintendo New 2DS XL', 4408])],
      dtype=object)
In [30]: np.hstack(np.array(d[0], object))                                                                   
Out[30]: 
array(['B01A8L6KKO', '873124006834',
       'HORI Premium Protector - Pikachu Edition for Nintendo New 2DS XL',
       '4408'], dtype='<U64')

虽然您从 d[0] 创建了一个对象数据类型数组,但 hstack 创建了一个字符串数据类型数组。

In [31]: np.hstack(np.array(d[0], object))[3]                                                                
Out[31]: '4408'

分配给此数组的任何内容都将变成字符串。

In [34]: x = np.hstack(np.array(d[0], object))                                                               
In [35]: x[3] = 123                                                                                          
In [36]: x                                                                                                   
Out[36]: 
array(['B01A8L6KKO', '873124006834',
       'HORI Premium Protector - Pikachu Edition for Nintendo New 2DS XL',
       '123'], dtype='<U64')

但是列表没有通用的dtype约束,所以元素可以变成整数:

In [37]: x = list(np.hstack(np.array(d[0], object)))                                                         
In [38]: x[3] = 123                                                                                          
In [39]: x                                                                                                   
Out[39]: 
['B01A8L6KKO',
 '873124006834',
 'HORI Premium Protector - Pikachu Edition for Nintendo New 2DS XL',
 123]

hstack 在将它们传递给 concatenate:

之前确保所有输入都是数组
In [49]: [np.atleast_1d(x) for x in d[0]]                                                                    
Out[49]: 
[array(['B01A8L6KKO'], dtype='<U10'), array(['873124006834',
        'HORI Premium Protector - Pikachu Edition for Nintendo New 2DS XL',
        '4408'], dtype='<U64')]

这解释了为什么 hstack 的结果是字符串 dtype。不需要 np.array(d[0], object) 步骤。

list() 包装器的替代方法是将字符串 dtype 转换为对象类型:

In [52]: x = np.hstack(d[0]).astype(object)                                                                  
In [53]: x[3] = 123                                                                                          
In [54]: x                                                                                                   
Out[54]: 
array(['B01A8L6KKO', '873124006834',
       'HORI Premium Protector - Pikachu Edition for Nintendo New 2DS XL',
       123], dtype=object)

tolist 通常更适合从数组创建列表,但在这里并没有太大区别:np.hstack(d[0]).tolist()

另一种扁平化列表的方法是:

In [62]: x = np.hstack([np.array(j, object) for j in d[0]])                                                  
In [63]: x                                                                                                   
Out[63]: 
array(['B01A8L6KKO', '873124006834',
       'HORI Premium Protector - Pikachu Edition for Nintendo New 2DS XL',
       4408], dtype=object)

x[3] 仍然是一个整数。

但您也可以直接将列表展平(因为它们都由一个字符串和一个列表组成):

In [66]: [d[0][0], *d[0][1]]                                                                                 
Out[66]: 
['B01A8L6KKO',
 '873124006834',
 'HORI Premium Protector - Pikachu Edition for Nintendo New 2DS XL',
 4408]