0

I have data for 12 instruments grouped as 3x4 (4 instruments for each group: N1, N2, N3, N4; C1, C2, C3, C4; S1, S2, S3, S4;). Each instrument has 2 sets of data: H and T. The dataset comes from different excel spreadsheets.

What is the best way to perform this interaction? Creating a dictionary, a matrix, using the lists...? In Matlab I would create 3 matrices with 4 columns each, but I don't know how to do this on Python (just started using it).

First I am trying to create the variables described above (N1, C3, S4...) and appending the values from different excel spreadsheets:

sheets = ['S_WH', 'C_WH', 'N_WH']
for i in sheets:
    path = r"\\uniwa.uwa.edu.au\userhome\students2\22371812\My Documents\Documents\FieldTechniques"
    filename = i + '.xlsx'
    sheetHeights = openpyxl.load_workbook(os.path.join(path,filename), data_only=True)
    heights = sheetHeights.get_sheet_by_name("Sheet1")

    Time=[]
    H1=[]
    H2=[]
    H3=[]
    H4=[]

    for row in range (2, heights.max_row+1):
        Time.append(heights['A' + str(row)].value)
        H1.append(heights['B' + str(row)].value)
        H2.append(heights['C' + str(row)].value)
        H3.append(heights['D' + str(row)].value)
        H4.append(heights['E' + str(row)].value)

First of all, how do I get those lists (H1, H2...) and append to a dictionary (that's how I thought it could be the best way) for each one of the sheets?

Secondly, I have to do the same for T values, so all the stations will have H and T related to it. A third value would be S, that is the sum of H and T. I don't know how to do this either. If someone could help me (even giving some hints) it would be great.

Nery Neto
  • 59
  • 1
  • 7

1 Answers1

0

Best is a bit ambiguous here. Lists have their pros & cons, and so do dictionaries, arrays, and every other data structure.

The best structure depends on what you need (minimal memory, minimal search time and so on).

As long as no major constraint is introduced, I would go with what I think is the simplest solution - dictionary that holds a key and a 3-values list as its value - {instrument : [H,T,S]}

Toy example:

N1,N2 = 'a','b'
H_N1, H_N2 = 1,2
T_N1, T_N2 = 10,20

instruments = [N1, N2]
# assuming you already have H,T saved
H = [H_N1, H_N2]
T = [T_N1, T_N2]

# create an list S, and fill it using list comprehension
S = [H[i] + T[i]  for i in range(len(T))]

# can also be done by a loop
S = []
for i in range(len(T)):
    S.append(T[i] + H[i])

# add the lists together so they would all be stored together as a dictionary value
zip1 = zip(H,T,S)
zip2 = zip(instruments, zip1)
d = dict((key,value) for (key,value) in zip2)
print d

Output = {'a': (1, 10, 11), 'b': (2, 20, 22)}

CIsForCookies
  • 12,097
  • 11
  • 59
  • 124
  • That's a good way. I am trying to fill the S for each instrument (H+T) but I don't know how to build the loop. Sorry, I know this is basic, but I am new in Python, after years on Matlab – Nery Neto Apr 26 '18 at 04:08
  • show how you would matlab it, or better yet, show your erroneous python loop – CIsForCookies Apr 26 '18 at 04:11