循环将列表列表分配到数组中

时间:2016-11-24 19:45:35

标签: python arrays list loops numpy

我有一个列表列表,我想将每个子列表分配给一个特定形状的形状数组。到目前为止,我有:

N=23

# read the file in lines
with open('ircforward.xyz','r') as f:
    lines = f.read().splitlines()

# Split the whole file into a list of lists 
profiles = [lines[i:i + N] for i in xrange(0, len(lines), N)]

#Convert first sublist to array skipping the first two lines    
set1 = np.genfromtxt(profiles[0],skip_header=2,usecols=[1,2,3])

有没有办法可以生成一堆名为“set_n”的数组,其中n将是一个索引变量,并且有以下几行:

for n in range (4000):
      set_n = np.genfromtxt(profile[n], skip_header=2, usecols=[1,2,3])

1 个答案:

答案 0 :(得分:2)

最好的解决方案是按如下方式使用字典:

object

现在,您可以使用<object>my_dict = {} for n in range (4000): my_dict['set_{}'.format(n)] = np.genfromtxt(profile[n], skip_header=2, usecols=[1,2,3]) 等来代替使用set_1set_2