0

I made some variables (list type) with using 'setattr' and 'sys.moduels[_ _ name _ _]'.

std_index_lists = {'name1':[8,9,55,193,168,417,285],
                   'name2':[55, 107, 9, 8, 193, 221, 222, 65],
                   'name3':[285, 9, 336, 295, 442, 441, 417, 8]}


for name in std_index_lists.keys():
    setattr(mod, "{}_face_2d".format(name), [])
    setattr(mod, "{}_face_3d".format(name), [])
    if 8 in std_index_lists[name]:
        getattr(mod, "{}_face_2d".format(name)).append([56,75])
        getattr(mod, "{}_face_3d".format(name)).append([3,4,7])
        setattr(mod, "{}_face_2d".format(name), (np.array(getattr(mod, '{}_face_2d'.format(name)), dtype=np.float64)))
        setattr(mod, "{}_face_2d".format(name), (np.array(getattr(mod, '{}_face_3d'.format(name)), dtype=np.float64)))

and the last 2 lines didn't work.

That lists are still list types.

I'd like to change lists to numpy array type... Please help me. Thank you.

정지윤
  • 11
  • 2
  • Short answer **don't**, this is a very bad practice, you will have issues (you already have issues!). Use a dictionary! – mozway May 17 '22 at 07:50
  • I almost forget it... The 'mod' variable is this. '\n'import numpy as np '\n'import sys '\n'mod = sys.modules[__name__] – 정지윤 May 17 '22 at 07:50
  • @mozway Okay.... I'll keep searching for another way..... thank you – 정지윤 May 17 '22 at 07:53
  • I fixed my code: I made 2 lists (temp1 = [], temp2 = []) and I append some numbers and I turned it into numpy array., and then I used 'setattr' and assigned arrays to dynamic variables. – 정지윤 May 18 '22 at 00:49

0 Answers0