def union_list(l1, l2): result = [] if not l1: result.extend(l2) return result if not l2: result.extend(l1) return result result.extend(l1) for i in set(l1+l2): if i not in l1: result.append(i) return result
set(l1+l2) 也可以达到以上目的,但是会重新排序。上述程序保留l1的顺序。
set 的操作:
联合(Union) 交集(Intersection) 差集(Difference)
set的减法与被减数相关,等同set.difference(): all elements that are in this set but not the others.
时间: 2024-10-14 04:45:56