Y = [1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1,]
X = [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 ]
Di = []
for number in Y: # as for each number in Y
if number > 0: # and the number if more than 0
if number in X: # compare to the numbers in X
if number not in Di: # when in cases of number found not in X
Di.append(number) # Add to the Di
print("Di",Di) #print the letters Di and the value of the number 1 found
if Di == [1]: # Condition #if the value is equal to 1 print excluded
print("excluded") # Clause #print excluded
else:
print("not excluded")输出
Di --> excludeed #because of digit 5 counting 0上面的代码来自python程序,它试图比较1或0,如下所示,找到Y列表中的第一个0值,并将其与列表X中相同位置的值进行比较,如果发现肯定,则排除,如果没有发现否定,则转到列表Y中的下一个0,依此类推,当列表完成时,不能排除任何未排除的打印。希望这是有意义的,但它没有发挥它的作用。如果有人可以用.index为我修改它,使其具有预期的功能,这将使我的生活更快乐。kind考虑基础
发布于 2020-10-24 16:12:33
Y = [1, 1, 0, 1, 0]
X = [0, 0, 1, 0, 0]
def excluder(yVal, xVal):
for count, ele in enumerate(yVal):
print("Current Y list value in this loop cycle:" + str(ele))
if yVal[count] > xVal[count]:
print(str(yVal[count]), " is greater in the Y list")
elif yVal[count] < xVal[count]:
print(str(xVal[count]), " is greater in the X list")
else:
print(str(xVal[count]), " X and Y are equal")
excluder(Y, X)下面的输出...我包含了一个枚举函数,因为它是在循环中获得计数的好方法,而不必递增变量。
Current Y list value in this loop cycle:1
1 is greater in the Y list
Current Y list value in this loop cycle:1
1 is greater in the Y list
Current Y list value in this loop cycle:0
1 is greater in the X list
Current Y list value in this loop cycle:1
1 is greater in the Y list
Current Y list value in this loop cycle:0
0 X and Y are equalhttps://stackoverflow.com/questions/64511054
复制相似问题