for i in :
for j in :
for k in :
for m in :
print(i,j,k,m)
那还是用个字典存起来,发现itertools模块有计算笛卡尔积的工具
from itertools import product
pins = {1:, 2:, 3:, 4:, 5:, \
6:, 7:, 8:, 9:, 0:}
pin = '1357'
# pin = input("请输入密码:")
try:
# 将输入生成二维列表,再解包,用product工具将笛卡尔积打印出来
for i in product(* for j in pin]):
print(i)
except ValueError:
print("请输入0-9正整数集合!")
然后再试下不用工具,怎么求笛卡尔积,结果只能想到用神奇的eval()函数凑出来{:10_266:}
def cartesian_product(times,sets):
"""参数times是列表个数,sets是包含各个列表的列表,返回笛卡尔积"""
string_1 = "("
string_2 = ""
for i in range(times):
string_1 += "a"*(i+1) + ',' # 最后会多一个逗号,切片去掉
string_2 += " for "+ "a"*(i+1)+" in "+str(sets) # 确保关键字左右有空格
# 把字符串组合成列表推导式
string = "[" + string_1[:-1] + ")" + string_2 + "]"
output = eval(string)
print(*output)
a = []
pin = input("请输入数字,我没有写防错,你输入了其他就直接报警哦:")
for j in pin:
a.append(pins)
cartesian_product(len(a),a) 答案 大佬你的题目都好绕啊。。。基本看一遍是看不明白的 import itertools as t
def getPINs(pin):
pin = list(pin)
for i in range(len(pin)):
if int(pin) == 1:
pin =
elif int(pin) == 2:
pin =
elif int(pin) == 3:
pin =
elif int(pin) == 4:
pin =
elif int(pin) == 5:
pin =
elif int(pin) == 6:
pin =
elif int(pin) == 7:
pin =
elif int(pin) == 8:
pin =
elif int(pin) == 9:
pin =
else:
pin =
pins = []
for i in t.product(pin,pin,pin,pin):
pins.append(i)
print(pins)
'''pin = input('间谍看到的数字:')'''
pin = '1357'
getPINs(pin)
不知道怎么把product里面加入未知数量的iter 有点意思,成功是成功了,但是不好
## 代码
import test
import itertools
def get_pins(x):
form = [,,,,,,,,,]
n = len(x)
y = []
result = []
for each in x:
y.append(int(each))
if n == 1:
comb = itertools.product(form])
elif n == 2:
comb = itertools.product(form],form])
elif n == 3:
comb = itertools.product(form],form],form])
elif n == 4:
comb = itertools.product(form],form],form],form])
elif n == 5:
comb = itertools.product(form],form],form],form],form])
else:
return '排列过大'
for i in comb:
z = ''
for j in i:
z += str(j)
result.append(z)
return result
## 测试
expectations = [('8', ['5','7','8','9','0']),
('11',["11", "22", "44", "12", "21", "14", "41", "24", "42"]),
('369', ["339","366","399","658","636","258","268","669","668","266","369","398","256","296","259","368","638","396","238","356","659","639","666","359","336","299","338","696","269","358","656","698","699","298","236","239"])]
for tup in expectations:
test.assert_equals(sorted(get_pins(tup)), sorted(tup), 'PIN: ' + tup)
## 结果
Success!
Success!
Success! 看看答案 学习学习 虽然有基本思路,但总会卡在某个地方想挺久的,看来还只是刚碰到python的一点点皮毛...{:10_269:}{:10_269:} 谢 捞 交作业。请问不用模块怎么计算不同长度和可变数量的笛卡尔积。想了一下午没头绪
import itertools
def getPins(PIN=1357):
dict1 = {'1': '124', '2': '2135', '3': '326', '4': '4157', '5': '52468', '6': '6359', '7': '748', '8': '80579',
'9': '968', '0': '08'}
temp = []
for each in str(PIN):
temp.append(dict1.get(each))# 每个数字可能的字符串
list1 = list(itertools.product(*temp))# 计算笛卡尔乘积,返回密码的元组
pinlist = [''.join(each) for each in list1]# 提取元组字符串拼接
print(pinlist)
return pinlist
def Fun36():
for i in :
for j in :
for k in :
for g in
print(i,j,k,g)
仅针对该题,的有效组合 from functools import reduce
password_dict = {'0': , '1': , '2': , '3': ,
'4': , '5': , '6': ,
'7': , '8': , '9': }
def combine_list(list1, list2):
return
def f_36(string: str) -> list:
iter_list = []
for i in string:
iter_list.append(password_dict)
return reduce(combine_list, iter_list)
print(f_36('1357'))
1 666
页:
1
[2]