直接上代码,有三种方法,第三种调用库函数效率最高 # ! /usr/bin/env python # encoding:utf-8 if __name__ == '__main__': a = [1,2,3,4,5] b = [2,3,6,7] u =[] dif =[] intersec = [] '''方法一,最简单的方法,容易想到的''' for item in a: u.append(item) if item in b: intersec.append(item) if item no…
python中对两个 list 求交集,并集和差集: 1.首先是较为浅白的做法: >>> a=[1,2,3,4,5,6,7,8,9,10] >>> b=[1,2,3,4,5] >>> intersection=[v for v in a if v in b] >>> intersection [1, 2, 3, 4, 5] >>> union=b.extend([v for v in a]) >>>…
'''Created on 2018年10月28日递归函数示例:阶乘'''def my_fun_example1(n): ''' 非递归函数求阶乘示例 ''' result = n for i in range(1,n): result *= i return resultdef my_fun_example2(n): ''' 递归函数求阶乘示例 ''' if n == 1: return 1 else…
package main import "fmt" func factorialFor(num int) (ret int) { // 循环求阶乘 ret = 1 for i := 1; i <= num; i++ { ret *= i } return } func factorialRecursion(num int) int { // 递归求阶乘 if num == 0{ return 1 } return num * factorialRecursion(num - 1)…