简体   繁体   中英

Assigning scores by comparing values in four lists

I have four lists. I need to compare each value that is there in perfect_list with each item in x,y and z and accordingly assign it a score. If the value in perfect_list is in x then assign a value x. If it is found in x and y then assign it 5. The problem here is that it is considering all the conditions. If it is found in x and y then it should print 5 only but its printing 3 and 5.

perfect_list=[0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 14, 16, 17, 18, 19, 20, 21, 22]
x=[0, 1, 2, 3, 4, 18, 19, 20, 21, 22, 100, 100, 100, 100, 100, 100, 100, 100]
y=[2, 3, 4, 8, 14, 17, 20, 21, 100, 100, 100, 100, 100, 100,  100, 100, 100, 100]
z=[5, 1, 6, 3, 7, 10, 16, 21, 22, 2, 8, 100, 100, 100, 100, 100, 100, 100]
score_list=[]
for n in perfect_list:
     if n in x:
        print "Present in x"
        score=3
        score_list.append(score)
        print score

     if n in y:
        print "Present in y"
        score=2
        score_list.append(score)
        print score
     if n in z:
        print "Present in z"
        score=1
        score_list.append(score)
        print score

     #elif n in x or n in y or n in z:
             #print "Not present in all"

     if n in x and n in y and n not in z:
        print "Present in x and y"
        score=5
        score_list.append(score)
        print score
     if n in x and n in z and n not in y:
        print "Present in x and z"
        score=4
        score_list.append(score)
        print score
     if n in y and n in z and n not in x:
        print "Present in z and y" 
        score=3
        score_list.append(score)
        print score
     if n not in x and n  in y and n  in z:
        print "Present in all"
        score=6
        score_list.append(score)
        print score
score_list.append(score)


Output:
Scores are:
[3, 3, 1, 4, 3, 2, 1, 3, 2, 1, 3, 2, 5, 1, 1, 1, 2, 1, 3, 6, 1, 2, 1, 2, 3, 3, 3, 2, 5, 3, 2, 1, 3, 1, 4, 4]
for n in perfect_list:
     if n in x and n in y and n in z:
             print "Present in all"
     if n in x or n in y or n in z:
             print "Not present in all"
     if n not in x and n not in y and n not in z:
             print "Not present in any"

You can use this syntax to check and later assign the score however you want to. Like:

if n in x:
    score=3

This answer is assuming the following

score if found in x = 3

score if found in y = 2

score if found in x = 1

score if found in x and y = score with x + score with y

score if found in y and z = score with y + score with z

score if found in x and z = score with x + score with z

score if found in x and y and z = score with x + score with y + score z

Note: The map function used here, maps over an array while performing a function over every item in the array, documentation can be found here https://www.programiz.com/python-programming/methods/built-in/map

#Declare Constants For Score
X_SCORE = 3
Y_SCORE = 2
Z_SCORE = 1

compare_list=[0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 14, 16, 17, 18, 19, 20, 21, 22]
x=[0, 1, 2, 3, 4, 18, 19, 20, 21, 22, 100, 100, 100, 100, 100, 100, 100, 100]
y=[2, 3, 4, 8, 14, 17, 20, 21, 100, 100, 100, 100, 100, 100,  100, 100, 100, 100]
z=[5, 1, 6, 3, 7, 10, 16, 21, 22, 2, 8, 100, 100, 100, 100, 100, 100, 100]

#Function calculateScore accepts an item from the array for each iteration of the array
def calculateScore(item):
    score = 0
    if item in x:
        score = X_SCORE
    if item in y:
        score = Y_SCORE
    if item in z:
        score = Z_SCORE
    if item in x and item in y:
        score = X_SCORE + Y_SCORE
    if item in x and item in z:
        score = X_SCORE + Z_SCORE
    if item in y and item in z:
        score = Y_SCORE + Z_SCORE
    if item in y and item in x and item in z:
        score = X_SCORE + Y_SCORE + Z_SCORE
    return score

# map(functionToPerformAction, ArrayToPerformActionOn)
# This will return an array
final_score = map(calculateScore, compare_list)

print(list(final_score))

# Output
# [3, 4, 6, 6, 5, 1, 1, 1, 3, 1, 2, 1, 2, 3, 3, 5, 6, 4]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM