朋友们,如需转载请标明出处:https://blog.csdn.net/jiangjunshow
声明:在人工智能技术教学期间,不少学生向我提一些python相关的问题,所以为了让同学们掌握更多扩展知识更好地理解AI技术,我让助理负责分享这套python系列教程,希望能帮到大家!由于这套python教程不是由我所写,所以不如我的AI技术教学风趣幽默,学起来比较枯燥;但它的知识点还是讲到位的了,也值得阅读!PS:看不懂本篇文章的同学请先看前面的文章,循序渐进每天学一点就不会觉得难了!
集合操作有各种各样常见的用途。例如,由于项在集合中只能存储一次,集合(set)可以用来把重复项从列表(list)中过滤掉。直接把列表(list)转换为一个集合(set),然后再转换回来即可:
>>> L = [1,2,1,3,2,4,5]
>>> set(L)
{1,2,3,4,5}
>>> L = list(set(L)) # Remove duplicates
>>> L
[1,2,3,4,5]
除了上面的操作外,下面我还给大家展示了更多有关集合的操作。这些操作应用于一个公司的人员名单:
>>> engineers = {'bob','sue','ann','vic'}
>>> managers = {'tom','sue'}
>>> 'bob' in engineers # bob是工程师吗?
True
>>> engineers & managers # 谁既是工程师又是经理?
{'sue'}
>>> engineers | managers # All people in either category
{'vic','sue','tom','bob','ann'}
>>> engineers - managers # Engineers who are not managers
{'vic','bob','ann'}
>>> managers - engineers # Managers who are not engineers
{'tom'}
>>> engineers > managers # Are all managers engineers? (superset)
False
>>> {'bob','sue'} < engineers # Are both engineers? (subset)
True
>>> (managers | engineers) > managers # All people is a superset of managers
True
>>> managers ^ engineers # Who is in one but not both?
{'vic','bob','ann','tom'}
>>> (managers | engineers) - (managers ^ engineers) # Intersection!
{'sue'}
转载:https://blog.csdn.net/jiangjunshow/article/details/116372770
查看评论