代码之家  ›  专栏  ›  技术社区  ›  Rebel

类型错误:'数字32'对象不可iterable,尽管正确生成了输出

  •  -1
  • Rebel  · 技术社区  · 6 年前

    在大型python脚本的某些行中,有一个74537000个元素的3d数组,这些元素都是浮点数:

    import numpy as np
    prop_shape = [74537000, 3]  # to produce this however a long script is written and this will be printed (just before the controversial line below) in the first run of the code assuming it's a vector quantity like position.
    prop_in_dtype = np.float32  # position array with the shape (74537000, 3) is made up of float32 values. In general, however, prop_in_dtype is changing across the script due to the nature of the array
    

    当我们试图创建一个元素都为-1的相同数组时,我们可以使用numpy,如下所示:

    .
    .
    .
    my_array = np.full(tuple(prop_shape), -1, prop_in_dtype)
    .
    .
    .
    

    运行此操作时,出现以下错误消息:

    Traceback (most recent call last):
      File "DLA_DM.py", line 19, in <module>
        settings_centroid.init()
      File "/usr5/username/settings_centroid.py", line 43, in init
        part=gizmo.io.Read.read_snapshots(species, snapshot_value_kind, snapshot_number, simulation_directory='.', snapshot_directory='output/', simulation_name='', properties=properties, element_indices=None, particle_subsample_factor=0, separate_dark_lowres=True, sort_dark_by_id=False, convert_float32=True, host_number=1, assign_host_coordinates=True, assign_host_principal_axes=False, assign_host_orbits=False, assign_formation_coordinates=False, assign_pointers=False, check_properties=True)
      File "/usr5/username/simulation/gizmo/gizmo_io.py", line 649, in read_snapshots
        element_indices, convert_float32, header)
      File "/usr5/username/simulation/gizmo/gizmo_io.py", line 1164, in read_particles
        part[spec_name][prop] = np.full(tuple(prop_shape), - 1, prop_in_dtype) 
    TypeError: 'numpy.uint32' object is not iterable
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   hpaulj    6 年前

    我可以用以下方法再现错误消息:

    In [174]: tuple(np.uint32(1))                                                        
    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <ipython-input-174-3420675a1788> in <module>
    ----> 1 tuple(np.uint32(1))
    
    TypeError: 'numpy.uint32' object is not iterable
    

    所以我猜这辆车有问题 prop_shape

        2
  •  0
  •   Rebel    6 年前

    prop_shape 在原始代码中迭代。有时是单个整数(对于标量量,例如74537000),有时是两个元素的列表(对于向量量,例如[74537000,3])。我假设它是固定的两元素列表(因此是一个向量)。这就是为什么单个标量的元组如果标量不是 float32 if statement 每次在生成数组之前检查变量的数据类型时:

    if isinstance(prop_shape, list):
        my_array = np.full(tuple(prop_shape), -1, prop_in_dtype)
    else:
        my_array = np.full(prop_shape, -1, prop_in_dtype)
    

    Traceback (most recent call last):
      File "DLA_DM.py", line 96, in <module>
        gizmo.plot.Image.plot_image(settings_centroid.part, 'dark', 'mass', settings_centroid.image_kind, [0,1], settings_centroid.dimensions_select, settings_centroid.distance_max, settings_centroid.distance_bin_width_generic, distance_bin_number=settings_centroid.distance_bin_number, part_indices=part_indices, write_plot=write_plot, plot_directory=plot_directory, background_color=background_color, use_column_units=False, center_position=settings_centroid.center_position, rotation=settings_centroid.rotation, property_select={'mass.bound':[1e9,1e13]}, subsample_factor=subsample_factor, image_limits=settings_centroid.image_limits_xy, hal=settings_centroid.hal, hal_indices=settings_centroid.hal_indices, hal_position_kind=hal_position_kind, hal_radius_kind=hal_radius_kind, return_halo_info=settings_centroid.return_halo_info_value_dark)
      File "/usr5/username/simulation/gizmo/gizmo_plot.py", line 825, in plot_image
        settings_centroid.init()
      File "/usr5/username/settings_centroid.py", line 43, in init
        part=gizmo.io.Read.read_snapshots(species, snapshot_value_kind, snapshot_number, simulation_directory='.', snapshot_directory='output/', simulation_name='', properties=properties, element_indices=None, particle_subsample_factor=0, separate_dark_lowres=True, sort_dark_by_id=False, convert_float32=True, host_number=1, assign_host_coordinates=True, assign_host_principal_axes=False, assign_host_orbits=False, assign_formation_coordinates=False, assign_pointers=False, check_properties=True)
      File "/usr5/username/simulation/gizmo/gizmo_io.py", line 649, in read_snapshots
        element_indices, convert_float32, header)
      File "/usr5/username/simulation/gizmo/gizmo_io.py", line 1171, in read_particles
        part[spec_name][prop] = np.full(prop_shape, -1, prop_in_dtype)
      File "/usr/local/anaconda3/lib/python3.5/site-packages/numpy/core/numeric.py", line 309, in full
        a = empty(shape, dtype, order)
    MemoryError