Home > Net >  Python - another try at 'base norming' data in an array, better explanations
Python - another try at 'base norming' data in an array, better explanations

Time:03-20

I apologize for unclear explanations in my previous posts. Here is a more thorough breakdown.

Here is a list:

lista3 = [2,3,4,5,6,7,8,9,10,2,2,4]

Now imagine that I want to use the first element, 2, as a base, and then norm the rest of the elements in the list to represent the delta with that first element. That means that the first element in the new list would be 100, and the others would be factors of that first base.

Here is the code

listNorm = []
for a in lista3:
    a = (a/lista3[0])*100
    listNorm.append(a)
print(listNorm)

The result is listNorm =

[100, 150, 200, 250, 300, 350, 400, 450, 500, 100, 100, 200]

So far so good! But now I want to do the same thing to a 2D array, like this one:

lista = [['countries', 2019, 2021, 2022],['aruba', 2,13,8],['barbados', 6,34,39],['japan', 12,8,16]]

Now I want to append this 2D array to a new 2D normed Array. The first row obviously needs to stay static. The first column obviously needs to stay static. Starting at the second row and second column, would be that first base element, to which the other elements in the list, row by row, would be 'normed'. So in other words, the second column, starting a row two, would be the base, which would have a value of 100 for each element in the column. The other elements in each row, would be normed to that element. And this row by row, for each country, so that I can see the progression over the years. Just like in the first list at the very top.

The result would look like this

listaNormed = [['countries', 2019, 2021, 2022],['aruba', 100,650,400],['barbados', 100,566,650],['japan', 100,66,133]]

How do I do that? I just get totally bogged down in all my attempts. Thank you!

CodePudding user response:

You are on the right track. But, since you are working with list of lists, you need to change your for loop and add a few other logic statements:

lista = [['countries', 2019, 2021, 2022],['aruba', 2,13,8],['barbados', 6,34,39],['japan', 12,8,16]]
for index,tempList in enumerate(lista):
  if tempList[0] == "countries":
    continue
  else:
    baseValue = None
    newList = []
    for value in tempList:
      if isinstance(value, str):
        newList.append(value)
        continue
      else:
        if baseValue == None:
          baseValue = value
          newList.append(baseValue)
        else:
          newList.append(int((value/baseValue)*100))
      lista[index] = newList
lista

Output

[['countries', 2019, 2021, 2022],
 ['aruba', 2, 650, 400],
 ['barbados', 6, 566, 650],
 ['japan', 12, 66, 133]]

Explnatation

enumerate changes a list to a list of tuples. The second element of each tuple is the value in the list, and the first element is the index of this value in the list. if tempList[0] == "countries": indicates the tempList is the list that contains the countries, ignore it and continue. isinstance is also a function that returns True if the first given element is an instance of the second element.

CodePudding user response:

Not the most concise it could be, but hopefully it makes sense.

lista = [['countries', 2019, 2021, 2022],['aruba', 2,13,8],['barbados', 6,34,39],['japan', 12,8,16]]
listb = []

for index, row in enumerate(lista):
    if index == 0:
        listb.append(row)
    else:
        normlist = [row[0]]
        for a in row[1:]:
            normlist.append(int((a/row[1])*100))
        listb.append(normlist)

normlist: [['countries', 2019, 2021, 2022], ['aruba', 100, 650, 400], ['barbados', 100, 566, 650], ['japan', 100, 66, 133]]

CodePudding user response:

And actually, I just was able to compress that to this.

lista = [['countries', 2019, 2021, 2022],['aruba', 2,13,8],
         ['barbados', 6,34,39],['japan', 12,8,16]]
listNorm = []

listNorm.insert(0,lista[0])

n = 1
while n < 4:
    for row in lista[n:(n 1)]:
        listNorm.append([])
        for colum in row[1:]:
            colum = int((colum/lista[n][1])*100)
            listNorm[n].append(colum)
    listNorm[n].insert(0, lista[n][0])
    n = n   1

print(listNorm)

CodePudding user response:

As this is for an absolute beginners class, I have a harder time understanding the first answer as I have not yet used some of those statement formats. I follow the second option a bit better, but I get lost at normList = [row[0]].

But i used some of the overall thinking to get to this clunky code, which still does the trick.

lista = [['countries', 2019, 2021, 2022],['aruba', 2,13,8],
         ['barbados', 6,34,39],['japan', 12,8,16]]
listNorm = []

listNorm.insert(0,lista[0])

for row in lista[1:2]:
    listNorm.append([])
    for colum in row[1:]:
        colum = int((colum/lista[1][1])*100)
        listNorm[1].append(colum)
    
for row in lista[2:3]:
    listNorm.append([])
    for colum in row[1:]:
        colum = int((colum/lista[2][1])*100)
        listNorm[2].append(colum)    
    
for row in lista[3:4]:
    listNorm.append([])
    for colum in row[1:]:
        colum = int((colum/lista[3][1])*100)
        listNorm[3].append(colum) 

listNorm[1].insert(0, lista[1][0])
listNorm[2].insert(0, lista[2][0])
listNorm[3].insert(0, lista[3][0])
print(listNorm)

So I will most likely use this as I grasp its basics. Still, thank you for much for taking me to the next level, I need to see how I can use enumerate more.

  • Related