Add nan to list

Viewed 44

I am trying to add nan values to mem_labels1 so it can have the same number of values as imaged 1. I thought this code would work but it still keeps a different number of values in each. How could i fix it?

  for id_code in list_of_done_files:
        if id_code in id_mem_dict:
            mem_labels1.append(id_mem_dict[id_code])
        else:
            mem_labels1.append(np.nan)
    
images1 = np.array(images1, dtype=object)
mem_labels1 = np.fromiter(id_mem_dict.values(), dtype=float)
mem_labels1 = np.array(mem_labels1)

1 Answers

You can pad your array:

mem_labels1 = np.pad(mem_labels1.astype(float),
                     (0, len(mem_labels1)-len(images1)),
                     'constant',
                     constant_values=(np.nan, np.nan))
Related