python编程的简洁代码

1.列表间元素操作

L1 = [1,3,5,]
L2 = [2,5,3,1,8]
x = set(L1)
y = set(L2)
#差集
print(y - x)
#交集
print(y&x)
#并集
print(y|x)

print(sorted(y - x)) #sorted函数作用是:返回一个新列表,包含以升序排列的所有迭代项。

结果:

  {8, 2}
  {1, 3, 5}
  {1, 2, 3, 5, 8}
  [2, 8]

  

猜你喜欢

转载自www.cnblogs.com/tang-s/p/9615037.html