python列表的交、並、差集


#!/usr/bin/env python3

l1 = ['1','2','3','4']
l2 = ['3','4','5','6']

# 交集 result1 = [i for i in l1 if i in l2] result2 = list(set(l1).intersection(set(l2))) print(result1) print(result2) # 並集 result3 = list(set(l1).union(set(l2))) print(result3) # 差集 # 元素在 l1 不在 l2 result4 = list(set(l1).difference(set(l2))) print(result4)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM