Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to print each item in a list’s score

I have a list where I assigned a score for each item in it.
For example,

    my_list = [1,2,3]
    fitnesses = []
    score = 0
    score1 = []
    score2 = []
    for x in my_list:
       If x > 1:
       score1 = score + 1
       if x > 2:
       score2 = score1 + 3
    for x in my_list:
          fitnesses.append(score1)
   print(fitnesses)

    I get [4,4,4] as an output
    I want to get [0,1,4]

I WANT to print each item’s score that’s in my_list, however for some reason I always get the last item in the list’s score repeated however long the list is. How do I get each item’s individual score?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Cleaning up a couple redundant variables as well as adjusting the initialization of score, we get the desired output:

my_list = [1,2,3]
fitnesses = []
for x in my_list:
    score = 0
    if x > 1:
        score += 1
    if x > 2:
        score += 3
    fitnesses.append(score)
print(fitnesses)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading