python list的一些用法总结

来源:互联网 发布:怎么封游戏端口 编辑:程序博客网 时间:2024/05/17 07:48

之前的一个用法用在代码里面了,可是在回顾代码的时候怎么也想不起来这种用法是啥东东了,最后想了老半天才弄明白(从反面来说明,写代码注释是多么重要呢)。

list差集a,b:

1.

ret = []

for i in a:

if i not in b:

ret.append(i)

2、

ret = [i for i in a if i not in b]

3、ret = list(set(a)^set(b))

list 并集

list(set(a).union(set(b)))

获取两个list的差集

list(set(b).difference(set(a))) # b中有而a中没有的

0 0
原创粉丝点击