码迷,mamicode.com
首页 > 编程语言 > 详细

python基础

时间:2019-05-06 23:37:33      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:pytho   计数器   port   strong   color   round   import   dde   ons   

寻找差异
li1=set([1,2,3,4])
li2=set([4,5,6,7])

update_set=li1.intersection(li2)
delete_set=li1.symmetric_difference(update_set)
add_set=li2.symmetric_difference(update_set)

print("update_set:", update_set)
print("delete_set:", delete_set)
print("add_set:", add_set)

li3=li1.difference(li2)
print("li3:",li3)

li4=li1.symmetric_difference(li2)
print("li4:",li4)

update_set: {4}
delete_set: {1, 2, 3}
add_set: {5, 6, 7}
li3: {1, 2, 3}
li4: {1, 2, 3, 5, 6, 7}

计数器
import collections

obj=collections.Counter(‘aaaaabbbbcccddeffgg‘)
ret=obj.most_common(4)

print(‘obj:‘,obj)
print(‘ret:‘,ret)

obj: Counter({‘a‘: 5, ‘b‘: 4, ‘c‘: 3, ‘d‘: 2, ‘f‘: 2, ‘g‘: 2, ‘e‘: 1})
ret: [(‘a‘, 5), (‘b‘, 4), (‘c‘, 3), (‘d‘, 2)]

python基础

标签:pytho   计数器   port   strong   color   round   import   dde   ons   

原文地址:https://www.cnblogs.com/zhucaipeng/p/10822800.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!