0

I have a density (dens) array of 156510 values which comes from 141 files with 1110 values in each (141*1110=156510). These files were all combined, but I need to generate a for loop to separate each file of range 1110. Below is what I want the for loop to do, so I don't have to keep doing this manually if more files are added. What would I need to do to the for loop to get this proper separation as I have listed below?

dens_1 = dens[0:1109]
dens_2 = dens[1110:2219]
dens_3 = dens[2220:3329]
dens_4 = dens[3330:4439]
...
dens_140 = dens[154290:155399]
dens_141 = dens[155400:156509]
martineau
  • 119,623
  • 25
  • 170
  • 301
Sjcarlso
  • 119
  • 11
  • Why do you want each one to be in a separate variable in the first place? – JeffUK Dec 15 '20 at 22:55
  • @JeffUK I'm trying to do a big interpolation and need these to be separated as shown above. – Sjcarlso Dec 15 '20 at 22:57
  • 1
    Do you really want separate variables for each range group? What about a list, where, say, your "dens_3" would be `dens_groups[2]` or a dict `{"dens_3":[...]}`? – tdelaney Dec 15 '20 at 23:01

1 Answers1

1

range is pretty good at generating ranges. You can create a list holding each range group as in

[dens[i:i+1110] for i in range(0, len(dens), 1110)]

or a dictionary like

{f"dens_{i+1}":dens[i:i+1110] for i in range(0, len(dens), 1110)}

you could even write a quick function to grab the ranges as you need them

def get_range(dens, range_index):
    return dens[i:i+1110]
tdelaney
  • 73,364
  • 6
  • 83
  • 116