wask
wask

Reputation: 1

Create new array variable inside loop in python3

r = 6371e3 # metres
latBS = 41.178210
lonBS = -8.597820
φ1 = latBS * math.pi/180 # φ, λ in radians
l = 0


dist=[] 
RSRP=[]

paths = ["pathA", "pathB", "pathC", "pathD0", "pathD1", "pathD2", "pathD3", "pathE", "pathF", "pathG0", "pathG1", "pathG2","pathG3"]

for x in paths:
    logFile = pd.read_csv('export/4G_{}.FMT'.format(x), sep='\t')
    logClean = logFile.dropna(subset=['All-Latitude', 'All-Longitude'])
 
    dist.clear()
    RSRP.clear()

    for index, row in logClean.iterrows():
    
        latMS=row['All-Latitude']
        lonMS=row['All-Longitude']

        φ2 = latMS * math.pi/180
        Δφ = (latMS-latBS) * math.pi/180
        Δλ = (lonMS-lonBS) * math.pi/180
        a = math.sin(Δφ/2) * math.sin(Δφ/2) + math.cos(φ1) * math.cos(φ2) * math.sin(Δλ/2) * math.sin(Δλ/2)
        c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
        d = r * c
        dist.append(d) # in metres
        RSRP.append(row['All-Serving Cell RSRP (dBm)[1]'])

With this current code, the 4G_pathA.FMT file is read and then processed in a loop where distance is measured between a pair of geographical coordinates. Those values are then stored in the array dist(). However, I would like to store those values in 13 different arrays, each one correspondent to each path in the paths[] array, so that dist_pathA, dist_pathB, etc. and RSRP_pathA, RSRP_pathB, etc. How can I create a new array inside a loop using the x value in the for loop as the name? I have tried

dist_{}.format(x)=[]

but to no avail.

Cheers

Upvotes: 0

Views: 43

Answers (1)

DevLounge
DevLounge

Reputation: 8437

example to store data inside arrays created/populated inside a loop:

from collections import defaultdict

arrays_dict = defaultdict(list)
for i in range(10):
  arrays_dict[i].append(<something>)

then after the loop ends, you have each key representing the array name and value the array content

Upvotes: 0

Related Questions