您好,欢迎访问一九零五行业门户网

Python List交集,并集,差集的应用

生成了两个list:
a = ['apple','apple','banana'] b = ['banana','apple','banana']

交集,并集,差集概念这里不说,python代码如下:
#! /usr/bin/env python # coding:utf-8 lista = [1, 2, 3, 4, 5, 6] listb = [4, 5, 6, 7] # intersection inte = list(set(lista).intersection(set(listb))) print intersection:, inte # union uni = list(set(lista).union(set(listb))) print union:, uni # differences diff = list(set(lista).difference(set(listb))) print differences:, diff

if diff:     print wrong else:     print matched

生成了两个list:
a = ['apple','apple','banana'] b = ['banana','apple','banana']

交集,并集,差集概念这里不说,python代码如下:
#! /usr/bin/env python # coding:utf-8 lista = [1, 2, 3, 4, 5, 6] listb = [4, 5, 6, 7] # intersection inte = list(set(lista).intersection(set(listb))) print intersection:, inte # union uni = list(set(lista).union(set(listb))) print union:, uni # differences diff = list(set(lista).difference(set(listb))) print differences:, diff

if diff:     print wrong else:     print matched

以上就是python list交集,并集,差集的应用的详细内容。
其它类似信息

推荐信息