首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在python中与.index单元格进行比较

如何在python中与.index单元格进行比较
EN

Stack Overflow用户
提问于 2020-10-24 15:33:44
回答 1查看 59关注 0票数 0
代码语言:javascript
运行
复制
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")

输出

代码语言:javascript
运行
复制
Di --> excludeed #because of digit 5 counting 0

上面的代码来自python程序,它试图比较1或0,如下所示,找到Y列表中的第一个0值,并将其与列表X中相同位置的值进行比较,如果发现肯定,则排除,如果没有发现否定,则转到列表Y中的下一个0,依此类推,当列表完成时,不能排除任何未排除的打印。希望这是有意义的,但它没有发挥它的作用。如果有人可以用.index为我修改它,使其具有预期的功能,这将使我的生活更快乐。kind考虑基础

EN

Stack Overflow用户

发布于 2020-10-24 16:12:33

代码语言:javascript
运行
复制
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)

下面的输出...我包含了一个枚举函数,因为它是在循环中获得计数的好方法,而不必递增变量。

代码语言:javascript
运行
复制
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 equal
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64511054

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档