Re: [问题] 有趣的问题 关于交集与联集的处理方式

楼主: darkgerm (黑骏)   2016-01-19 23:39:23
※ 引述《busystudent (busystudent)》之铭言:
: hi 我是Bob 第一次在这边发文,听说有不少高手卧虎藏龙!
: as title 我爬虫了一个网站,经过整理得到很多如下面的结果
: ['spanish', 'web2.0', 'e-learning', 'education', 'social', 'spain', 'tools', 'learning', 'google', 'e-learning2.0']
: ['education', 'technology', 'learning', 'classroom', '%22educational%20technology%22', 'google', 'teaching', 'collaboration', 'students', 'web2.0']
: [education]
: [technology]
: 每一个list就是一个人所收藏的标签,像是a桑收藏第一行的list标签集,b桑收藏第二行的标签集等,有很多笔list。
: 我想请问,要如何计算与判断同时收藏education标签与technology的人数,以及要如行计算与判断收藏education或收藏technology的人数
: 我一开始是打算先指定一个标签education,再使用if判断是否与目标technology同时出现,可是考虑到人数就又感觉到怪怪的
: 请大家给我一点提示! 谢谢
要判断 "同时收藏 education 及 technology" / "有收藏 education 或 echnology"
建意可以用 set 来做
data = [
['spanish', 'education', 'e-learning', ...],
['education', 'technology', 'learning', ...],
['education'],
['technology],
]
# 如果 data 本来不是 set,可以先转成 set
# 没特别需求的话,建意一开始就存成 set
data = [set(i) for i in data]
# 计算 "同时收藏 education 及 technology" (属于 ∈)
count = 0
for i in data:
if {'education', 'technology'} <= i:
count += 1
# 熟悉 functional programming 的话也可以这样写
count = len([i for i in data if {'education', 'technology'} <= i])
# 计算 "有收藏 education 或 technology" (交集 ∩)
count = 0
for i in data:
if {'education', 'technology'} & i:
count += 1
# 同样也可以这样写
count = len([i for i in data if {'education', 'technology'} & i])

Links booklink

Contact Us: admin [ a t ] ucptt.com