Apologies in advance as I am quite new to this all and I'm not certain on the correct terms for everything.
I have a list of differences between two lists, called Change
. I would like to generate a list of percentage changes Percentage
by dividing each value in Change
by the highest of the respective values in the lists it references (Before
and After
). If the wrong value is used, a percentage change is showing at -660% in some cases.
Before
and After
are generated from image files through PIL, but a small section of the output is below.
Before = [135,160,199,]
After = [146,174,176,]
Change = list(After-Before for Before,After in zip(Before,After))
PercentageA = list(Change/After*100 for After,Change in zip(After,Change))
PercentageB = list(Change/Before*100 for Before,Change in zip(Before,Change))
for x in Change:
if x <0:
Percentage = PercentageA
else:
Percentage = PercentageB
print(Percentage)
This code generates:
In [1]:
[8.148148148148149, 8.75, -11.557788944723619]
[8.148148148148149, 8.75, -11.557788944723619]
[7.534246575342466, 8.045977011494253, -13.068181818181818]
However, the result should be:
[7.534246575342466, 8.045977011494253, -11.557788944723619]
My main question is; how can I generate a Percentage
list from Change
divided by the highest of Before
and After
for each value in the list?
Edit: Reduced and generalised some of the code, and deleted background. Apologies for putting too much background in. Edit2: Apologies for misunderstanding what was asked of me.