'generator'对象不支持项目分配

时间:2018-03-07 22:53:48

标签: python python-2.7 numpy scipy

def _ext_x_y(buff_1):

    min_x=1000.0
    min_y=1000.0
    max_x=0.0
    max_y=0.0

    #buff_1 is a mat file

    tot_no_hd_img=buff_1['boxes'][0].shape[0]  
     #gives total no of lists within a list in buff_1 .mat file
    hds_info_arr2d=[(0.0 for col in range(4))for rows in range(tot_no_hd_img)]   
    #initializing 2d array

    for cur_hand in range(tot_no_hd_img):
        for i in range(4): # i represents total no of elements i a list
            try:
                idx_val_x=buff_1['boxes'][0][cur_hand][0][0][i][0][0]
                # last index[0] represents x coordinate float value
                if(idx_val_x<min_x):
                    min_x=idx_val_x
                    hds_info_arr2d[cur_hand][0]=min_x
                elif(idx_val_x>max_x):
                    hds_info_arr2d[cur_hand][1]=max_x

                idx_val_y=buff_1['boxes'][0][cur_hand][0][0][i][0][1]
                #last index[1]  represents y coordinate float value 
                if(idx_val_y<min_y):
                    hds_info_arr2d[cur_hand][2]=min_y
                elif(idx_val_y>max_y):
                    hds_info_arr2d[cur_hand][3]=max_y
            except Exception as e:
                print e


    return hds_info_arr2d #this should return a 2D list of x,y coordinates

image_mat_file=sc.loadmat('/home/ishaan/Codes/Datasets/hand_dataset/test_dataset/test_data/annotations/VOC2007_2.mat')

x_y=_ext_x_y(image_mat_file) #why is this generating error 
print x_y

问题: 我试图得到一个带有x,y坐标的二维列表,但它一直产生错误:

'generator' object does not support item assignment

'generator' object does not support item assignment

'generator' object does not support item assignment

'generator' object does not support item assignment

[<generator object <genexpr> at 0x7f2c5a5df140>]

1 个答案:

答案 0 :(得分:0)

你需要

hds_info_arr2d=[[0.0 for col in range(4)] for rows in range(tot_no_hd_img)] 

而不是

hds_info_arr2d=[(0.0 for col in range(4)) for rows in range(tot_no_hd_img)]