T.Y736 2021-03-17 13:26 采纳率: 100%
浏览 65
已采纳

有没有大佬救救我,这是为什么?

Elizabeth 的孙辈是:['Beatrice', 'James', 'William', 'Eugenie', 'Louise', 'Harry', 'Zara', 'Peter']
Traceback (most recent call last):
  File "D:\Download\Netease\python\family tree.py", line 161, in <module>
    print("Diana 的姐夫/妹夫是:"+str(BrotherInLaw("Diana")))
  File "D:\Download\Netease\python\family tree.py", line 74, in BrotherInLaw
    l1 = Sister(me) + Sister(spouse(me))  # 我所有的姐妹和我配偶的姐妹
  File "D:\Download\Netease\python\family tree.py", line 67, in Sister
    l2 = list(set(l2))
TypeError: 'NoneType' object is not iterable
[Finished in 0.4s with exit code 1]
from kanren import run, var
from kanren import Relation, facts

father = Relation()
mother = Relation()
#使用kanren定义基本关系列表,将树状图中的人物关系列举展现,我愿称此为真直系亲表
facts(father, ("Charles", "William"), ("Charles", "Harry"), ("Mark", "Peter"), ("Mark", "Zara")
      , ("Andrew", "Beatrice"), ("Andrew", "Eugenie"), ("Edward", "Louise"), ("Edward", "James")
      , ("Spencer", "Diana"), ("Spencer", "Charles"), ("Philip", "Anne"), ("Philip", "Andrew")
      , ("Philip", "Edward"), ("George", "Elizabeth"), ("George", "Margaret"))#定义父亲关系
facts(mother, ("Diana", "William"), ("Diana", "Harry"), ("Anne", "Peter"), ("Anne", "Zara")
      , ("Sarah", "Beatrice"), ("Sarah", "Eugenie"), ("Sophie", "Louise"), ("Sophie", "James")
      , ("Kydd", "Diana"), ("Elizabeth", "Charles"), ("Elizabeth", "Anne"), ("Elizabeth", "Andrew")
      , ("Elizabeth", "Edward"), ("Mum", "Elizabeth"), ("Mum", "Margaret"))#定义母亲关系
#创建男女列表,方便筛选
man = ["William", "Harry", "Peter", "James", "Charles", "Mark", "Andrew", "Edward", 
       "Spencer", "Philip", "George"]
woman = ["Zara", "Beatrice", "Eugenie", "Louise", "Diana", "Anne", "Sarah", "Sophie", 
         "Kydd", "Elizabeth", "Margaret", "Mum"]
def GrandChild(grandparent):  # 定义函数寻找“孙儿”
    #子女的子女,在创建的真直系亲表中寻找
    someone = var()
    someone_parent = var()
    l1 = list(run(0, someone, father(grandparent, someone_parent), father(someone_parent, someone)) +
              run(0, someone, mother(grandparent, someone_parent), father(someone_parent, someone)) +
              run(0, someone, father(grandparent, someone_parent), mother(someone_parent, someone)) +
              run(0, someone, mother(grandparent, someone_parent), mother(someone_parent, someone)))
    return l1
def Son(parent):  # 定义函数寻找儿子
	#子女,在创建的真直系亲表中寻找
    someone = var()
    l1 = list(run(0, someone, father(parent, someone)) +
              run(0, someone, mother(parent, someone)))
    for i in l1:
        if i in woman:
            l1.remove(i)
    return l1
def Daughter(parent):  # 定义函数寻找女儿
 	#子女,在创建的真直系亲表中寻找
    someone = var()
    l1 = list(run(0, someone, father(parent, someone)) +
              run(0, someone, mother(parent, someone)))
    l2 = []
    for i in l1:
        if i in woman:
            l2.append(i)
def spouse(one):  # 定义函数寻找配偶
    another = var()
    son = Son(one)[0]
    l1 = list(run(0, another, father(another, son)) + run(0, another, mother(another, son)))
    l1.remove(one)
    return l1[0]
def Brother(me):  # 同父母的兄弟,亲兄弟
    # 父母的儿子(除自己外)
    my_parent = var()
    l1 = list(run(0, my_parent, father(my_parent, me)))         # 获得父亲
    l2 = Son(l1[0])    # 求出父母的儿子
    l2 = list(set(l2))
    if me in l2:            # 除掉自己
        l2.remove(me)
    return l2

def Sister(me):  # 同父母的姐妹,亲姐妹,除自己外
    my_parent = var()
    l1 = list(run(0, my_parent, father(my_parent, me)))  # 获得父亲
    l2 = Daughter(l1[0])  # 求出父母的女儿
    l2 = list(set(l2))
    if me in l2:  # 除掉自己
        l2.remove(me)
    return l2

def BrotherInLaw(me):  # 定义函数寻找姐夫/妹夫
    someone = var()
    l1 = Sister(me) + Sister(spouse(me))  # 我所有的姐妹和我配偶的姐妹
    l2, l3 = [], []
    for i in l1:
        l2 = l2 + Son(i) + Daughter(i)  # 我姐妹所有的子女
    for i in l2:
        l3 = l3 + list(run(0, someone, father(someone, i)))  # 每个子女们的父亲,即姐夫
    l3 = list(set(l3))
    return l3

def SisterInLaw(me):  # 定义函数寻找嫂子/弟妹
    someone = var()
    l1 = Brother(me) + Brother(spouse(me))  # 我所有的兄弟和我配偶的兄弟
    l2, l3 = [], []
    for i in l1:
        l2 = l2 + Son(i) + Daughter(i)  # 我兄弟所有的子女
    for i in l2:
        l3 = l3 + list(run(0, someone, mother(someone, i)))  # 每个子女们的母亲,即嫂子/弟妹
    l3 = list(set(l3))
    return l3
def FirstCousin(me):  # 父母的亲兄弟亲姐妹的孩子(即“堂”的关系)
    my_parent = var()
    l1 = Brother(list(run(0, my_parent, father(my_parent, me)))[0]) + \
         Sister(list(run(0, my_parent, father(my_parent, me)))[0])
    l2 = Brother(list(run(0, my_parent, mother(my_parent, me)))[0]) + \
         Sister(list(run(0, my_parent, mother(my_parent, me)))[0])
    l3 = []
    for i in l1:
        l3 = l3 + Son(i) + Daughter(i)
    for j in l2:
        l3 = l3 + Son(j) + Daughter(j)
    l3 = list(set(l3))
    return l3
def Aunt(me):  # 与父母亲同辈的女性亲属,姑表亲
    parent = var()
    l1 = FirstCousin(me)
    l2 = []
    for i in l1:
        l2 = l2 + list(run(0, parent, mother(parent, i)))
    l2 = list(set(l2))
    return l2
def Uncle(me):  # 与父母亲同辈的男性亲属
    parent = var()
    l1 = FirstCousin(me)
    l2 = []
    for i in l1:
        l2 = l2 + list(run(0, parent, father(parent, i)))
    l2 = list(set(l2))
    return l2
def GreatGrandparent(grandson):  # 曾祖父母  
    # 父亲的父亲的父亲  母亲的母亲的母亲
    # 父亲的父亲的母亲  母亲的母亲的父亲
    # 父亲的母亲的父亲  母亲的父亲的母亲
    # 父亲的母亲的母亲  母亲的父亲的父亲
    someone = var()
    someone_son = var()
    someone_son_son = var()
    l1 = list(run(0, someone, father(someone, someone_son), father(someone_son, someone_son_son),
                  father(someone_son_son, grandson)) +
              run(0, someone, father(someone, someone_son), father(someone_son, someone_son_son),
                  mother(someone_son_son, grandson)) +
              run(0, someone, father(someone, someone_son), mother(someone_son, someone_son_son),
                  father(someone_son_son, grandson)) +
              run(0, someone, father(someone, someone_son), mother(someone_son, someone_son_son),
                  mother(someone_son_son, grandson)) +
              run(0, someone, mother(someone, someone_son), mother(someone_son, someone_son_son),
                  mother(someone_son_son, grandson)) +
              run(0, someone, mother(someone, someone_son), mother(someone_son, someone_son_son),
                  father(someone_son_son, grandson)) +
              run(0, someone, mother(someone, someone_son), father(someone_son, someone_son_son),
                  mother(someone_son_son, grandson)) +
              run(0, someone, mother(someone, someone_son), father(someone_son, someone_son_son),
                  father(someone_son_son, grandson)))
    return l1
def Ancestor(me):  # 祖先 比爷爷辈还要高一辈的并非血缘关系的所有长辈 曾祖父母及以上
    parent = var()
    l1 = GreatGrandparent(me)
    l2 = []
    l2 = l2 + l1
    while l1:       # 循环判断是否有上一代
        for i in l1:
            l3 = list(run(0, parent, father(parent, i))) + list(run(0, parent, mother(parent, i)))
            # 祖先的父母是否存在于关系中
            if l3:
                l2 = l2 + l3
            l1.remove(i)
    return l2
print("Elizabeth 的孙辈是:"+str(GrandChild("Elizabeth")))
print("Diana 的姐夫/妹夫是:"+str(BrotherInLaw("Diana")))
print("Zara 的曾祖父母是:"+str(GreatGrandparent("Zara")))
print("Eugenie 的祖先是:"+str(Ancestor("Eugenie")))
  • 写回答

1条回答 默认 最新

  • 芫尘梦 2021-03-17 13:45
    关注

    l2 = Daughter(l1[0]) # 求出父母的女儿  这里的l2是None

    Daughter(parent)方法 最后加上 return l2 就可以了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制