{ "question_id": 864358, "question_title": "请问如何用python做两个数组逐位判断?", "question_content": "比如有以下数组:\na1: 1,0,0,1,0,0,0,1\na2: 0,0,0,0,1,1,1,1\na3: 0,1,0,1,0,1,0,0\na4: 1,0,1,1,1,1,0,0\na5: .......\n抓取三个数组进行判断,\n if ((a1第一位or a2第一位 or a3第一位=1 )and (a1第二位 or a2 第二位 or a3第二位=1)and....\n直到判断完所有位数为止,所有位都有了1的话就输出当前这三个数组,已输出的数组不参与之后的判断。", "difficulty": "简单", "answer_id": 993346, "answer_content": "不知道抓取3个数组是任意3个,比如123 124 125 134 135 235...,还是顺序联系的3个,123 456 789这样抓。\n我是按照前者写的,如果不是,请说明\n```\n# -*- coding: UTF-8 -*-\nfrom itertools import combinations\n\na1=[ 1,0,0,1,0,0,0,1]\na2=[ 0,0,0,0,1,1,1,1]\na3=[ 0,1,0,1,0,1,0,0]\na4=[ 1,0,1,1,1,1,0,0]\na5=[ 1,1,1,1,1,1,1,0]\na6=[ 0,0,0,0,0,0,0,1]\na=[a1,a2,a3,a4,a5,a6]\n\nal = list(combinations(a,3))\nfor i in al:\n flag = True\n for j in range(len(i[0])):\n if (i[0][j] + i[1][j] + i[2][j] == 0):\n flag = False\n break\n if flag:\n print(i) \n\t\t\n```\n([1, 0, 0, 1, 0, 0, 0, 1], [0, 0, 0, 0, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 0])\n([1, 0, 0, 1, 0, 0, 0, 1], [0, 1, 0, 1, 0, 1, 0, 0], [1, 1, 1, 1, 1, 1, 1, 0])\n([1, 0, 0, 1, 0, 0, 0, 1], [1, 0, 1, 1, 1, 1, 0, 0], [1, 1, 1, 1, 1, 1, 1, 0])\n([1, 0, 0, 1, 0, 0, 0, 1], [1, 1, 1, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0, 1])\n([0, 0, 0, 0, 1, 1, 1, 1], [0, 1, 0, 1, 0, 1, 0, 0], [1, 0, 1, 1, 1, 1, 0, 0])\n([0, 0, 0, 0, 1, 1, 1, 1], [0, 1, 0, 1, 0, 1, 0, 0], [1, 1, 1, 1, 1, 1, 1, 0])\n([0, 0, 0, 0, 1, 1, 1, 1], [1, 0, 1, 1, 1, 1, 0, 0], [1, 1, 1, 1, 1, 1, 1, 0])\n([0, 0, 0, 0, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0, 1])\n([0, 1, 0, 1, 0, 1, 0, 0], [1, 1, 1, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0, 1])\n([1, 0, 1, 1, 1, 1, 0, 0], [1, 1, 1, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0, 1])\n\n----------------------------\n只使用1次\n\n```\n# -*- coding: UTF-8 -*-\nfrom itertools import combinations\n\na1=[ 1,0,0,1,0,0,0,1]\na2=[ 0,0,0,0,1,1,1,1]\na3=[ 0,1,0,1,0,1,0,0]\na4=[ 1,0,1,1,1,1,0,0]\na5=[ 1,1,1,1,1,1,1,0]\na6=[ 0,0,0,0,0,0,0,1]\na=[a1,a2,a3,a4,a5,a6]\nal = list(combinations(list(i for i in range(0, 6)),3))\nused = []\nfor i in al:\n\tflag = False\n\tif not(i[0] in used or i[1] in used or i[2] in used):\n\t\tflag = True\n\t\tfor j in range(7):\n\t\t\tif (a[i[0]][j] + a[i[1]][j] + a[i[2]][j] == 0):\n\t\t\t\tflag = False\n\t\t\t\tbreak\n\tif flag:\n\t\tused.append(i[0])\n\t\tused.append(i[1])\n\t\tused.append(i[2])\n\t\tprint(' '.join(map(str, a[i[0]])) + \"; \" + ' '.join(map(str, a[i[1]])) + \"; \" + ' '.join(map(str, a[i[2]])))\n\t\t\n```\n1 0 0 1 0 0 0 1; 0 0 0 0 1 1 1 1; 1 1 1 1 1 1 1 0\n", "tag_name": "python", "python": "# -*- coding: UTF-8 -*-\nfrom itertools import combinations\na1=[ 1,0,0,1,0,0,0,1]\na2=[ 0,0,0,0,1,1,1,1]\na3=[ 0,1,0,1,0,1,0,0]\na4=[ 1,0,1,1,1,1,0,0]\na5=[ 1,1,1,1,1,1,1,0]\na6=[ 0,0,0,0,0,0,0,1]\na=[a1,a2,a3,a4,a5,a6]\nal = list(combinations(a,3))\nfor i in al:\n\tflag = True\n\tfor j in range(len(i[0])):\n\t\tif (i[0][j] + i[1][j] + i[2][j] == 0):\n\t\t\tflag = False\n\t\t\tbreak\n\tif flag:\n\t\tprint(i)\t", "topic_link": "https://bbs.csdn.net/topics/600469990", "status": 1, "keywords": "算法初阶,基础知识,分治策略,最大子数组问题", "license": "csdn.net", "notebook": { "python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/python/40.ipynb?type=file" }, "notebook_enable": 1 }