下面的代码比较了三个列表motherList fatherlist
和sonList
,检查{motherList
或{
def compareList(motherList, fatherList, sonList):
count = 0
for x in sonList:
if x in motherList and x in fatherList:
count = 2
elif x in motherList or x in fatherList:
count += 1
if count >= 2:
ans = "Mendelion"
else:
ans = "Non-Medelian"
print"{0} {1} \t {2} \t {3}".format(motherList, fatherList, sonList, ans)
输出:
^{pr2}$有没有更简洁的方法来实现这一点? 可能是通过递归或非递归的方式
使用列表理解:
用套,我的朋友。在
所以你的代码看起来像:
^{pr2}$首先,它看起来真的很适合任何开发人员,其次它不是那么昂贵,但取决于数据大小。在
集合是python中的一种特殊类型,它使您能够找到交集(两个集合中都有值)和差异,这在许多情况下非常方便使用。在
你要找的是一个固定操作。Python集合已经强制要求所有项目都是唯一的,您可以执行以下操作:
在这两个集合中,父项和子项在集合中是对称的,但不是所有子项集合的差异。在
进一步阅读:https://docs.python.org/2/library/stdtypes.html#set-types-set-frozenset
相关问题 更多 >
编程相关推荐