Python:Set X和Set Y所有映射都是内射的和满射的

  • 本文关键字:Set 满射 Python 映射 python
  • 更新时间 :
  • 英文 :


设置X和Y所有映射->(无进口(

x=[0, 1],Y=[1,2]
=
[[(0, 1), (1, 1)], 
[(0, 2), (1, 1)], 
[(0, 1), (1, 2)], 
[(0, 2), (1, 2)]]
def maps(X, Y):
if X==[]:
return [[]]
if X!=[] and Y==[]:
return Y
def product(*args, repeat=1):
pools = [tuple(pool) for pool in args] * repeat
result = [[]]
for pool in pools:
result = [x+[y] for x in result for y in pool]
for prod in result:
yield tuple(prod)
e=[]
for i in product(*([Y]*len(X))):
e.append(list(zip(X, i)))
return e

这就是我现在所拥有的,但我想要这个用于内射和满射一次,所以定义映射内射和定义满射

您可以使用列表综合来过滤您已经拥有的函数返回的所有可能映射的列表,例如

def maps_surjective(X, Y):
return [f for f in maps(X, Y) if set(y for x, y in f) == set(Y)] 
def maps_injective(X, Y):
return [f for f in maps(X, Y) if len(set(y for x, y in f)) == len(X)]

最新更新