Home > Software design >  How to print each item in a list’s score
How to print each item in a list’s score

Time:01-02

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?

CodePudding user response:

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)
  • Related