我有四个清单。我需要将perfect_list中的每个值与x,y和z中的每个项目进行比较,并相应地为其分配一个分数。如果perfect_list中的值在x中,则分配一个值x。如果在x和y中找到它,则将其赋值为5。这里的问题是它正在考虑所有条件。如果在x和y中找到它,那么它应该只打印5,但是打印3和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]
答案 0 :(得分:0)
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"
您可以使用此语法进行检查,然后根据需要分配分数。喜欢:
if n in x:
score=3
答案 1 :(得分:0)
此答案假设以下情况
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
注意: 此处使用的map函数可在数组上进行映射,同时对数组中的每个项目执行功能,文档可在此处找到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]