从给定数组中随机抓取三个数组逐位比对
CSDN“每日一练”(python )题目
练习题目
代码运行效果
另一种运行可能
python完整代码
(如果从语句注释不能清楚作用,请评论区留言指教和探讨。🤝)
#/sur/bin/nve python
# coding: utf-8
import random, os
from itertools import combinations #加载排列组合模块
#三个数组逐位比对
a1 = [1, 0, 0, 1, 0, 0, 0, 1]
a2 = [0, 0, 0, 0, 1, 1, 1, 1]
a3 = [0, 1, 0, 1, 0, 1, 0, 0]
a4 = [1, 0, 1, 1, 1, 1, 0, 0]
a5 = [0, 1, 0, 1, 0, 1, 0, 1]
a6 = [1, 1, 0, 1, 1, 0, 1, 0]
a7 = [0, 0, 1, 0, 0, 1, 0, 1]
a8 = [1, 0, 1, 0, 1, 1, 0, 1]
a9 = [0, 0, 1, 1, 0, 1, 0, 0]
numbers = [a1, a2, a3, a4, a5, a6, a7, a8, a9]
ct = combinations #取别名
max = len(list(ct(numbers, 3)))
k = 1
while len(numbers)>=3:
l = os.system(r'clear') #清屏
print('\n'*6)
if k > max:
input(f' 比对次数超过最大组合数{max},程序将返回。\n\n')
break
print(f'{len(numbers)}个数组参与抓取,正在比较……\n\n'.center(36))
combis = list(ct(numbers, 3)) #排列组合
random.shuffle(combis) #随机洗牌
temp = random.choice(combis) #随机取组合
print('正在比对数组:')
[print(f'{i}') for i in temp]
if len(numbers)==3 and flag is False:
input(f'\n\n 最后三个数组不符合输出条件,程序将返回。')
break
for i in range(len(a1)):
if temp[0][i]==1 or temp[1][i]==1 or temp[2][i]==1:
flag = True
else:
flag = False
break
if flag is True:
for i in temp: #删除已打印数组
numbers.remove(i)
line = '﹊'*21 #一条语句定制输出最后结果
input(f'\n\n第{k}次比较:剩余数组{len(numbers)}个\n{line}\n符题数组:\n{temp[0]}\n{temp[1]}\n{temp[2]}\n{line}')
k += 1
combinations排列组合模块
今天学到排列组合模块combinations,在python内嵌工具itertools中。是今天在肖老板的博文《“Python使用combinations实现排列组合”》学到的。
。
加载语句:
from itertools import combinations
运行效果
我的博文推荐:
- 尼姆游戏(聪明版/傻瓜式•人机对战)(当前阅读3216)(代码优化版)
- 练习:字符串统计(坑:f‘string‘报错)(当前阅读1205)
- 练习:求列表(整数列表)平衡点(当前阅读1687)
- 密码强度检测器(当前阅读1002)
推荐条件
点阅破千
参考文章:
上一篇: 解析模块方法名
下一篇: 排序数组中查找元素的位置
![老齐漫画头像](https://i-blog.csdnimg.cn/blog_migrate/8cbf2c45d8b57205b31ef4963b3b9854.jpeg)
精品文章:
来源:老齐教室