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!
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:
Output
Explnatation
enumeratechanges 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 thetempListis the list that contains thecountries, ignore it and continue.isinstanceis also a function that returns True if the first given element is an instance of the second element.