首页 / PYTHON / python – 如何比较集群?
python – 如何比较集群?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – 如何比较集群?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1882字,纯文字阅读大概需要3分钟。
内容图文
![python – 如何比较集群?](/upload/InfoBanner/zyjiaocheng/786/3ece6f3e40c648bdaf5c98a2a926b702.jpg)
希望这可以用python完成!我在相同的数据上使用了两个集群程序,现在有两个集群文件.我重新格式化了文件,使它们看起来像这样:
Cluster 0:
Brucellaceae(10)
Brucella(10)
abortus(1)
canis(1)
ceti(1)
inopinata(1)
melitensis(1)
microti(1)
neotomae(1)
ovis(1)
pinnipedialis(1)
suis(1)
Cluster 1:
Streptomycetaceae(28)
Streptomyces(28)
achromogenes(1)
albaduncus(1)
anthocyanicus(1)
etc.
这些文件包含细菌种类信息.所以我有簇号(簇0),然后就在它’家族'(布鲁氏科)的正下方和该家族中的细菌数量(10).在那个下面是在该家族中发现的属(名称后跟数字,布鲁氏菌(10)),最后是每个属中的物种(流产(1)等).
我的问题:我有2个以这种方式格式化的文件,并希望编写一个程序来查找两者之间的差异.唯一的问题是两个程序以不同的方式聚类,因此两个聚类可能是相同的,即使实际的“聚类号”不同(因此一个文件中的聚类1的内容可能与另一个文件中的聚类43匹配,唯一不同的是实际的簇号).所以我需要忽略集群号并关注集群内容.
有什么方法可以比较这两个文件来检查差异吗?它甚至可能吗?任何想法将不胜感激!
解决方法:
鉴于:
file1 = '''Cluster 0:
giant(2)
red(2)
brick(1)
apple(1)
Cluster 1:
tiny(3)
green(1)
dot(1)
blue(2)
flower(1)
candy(1)'''.split('\n')
file2 = '''Cluster 18:
giant(2)
red(2)
brick(1)
tomato(1)
Cluster 19:
tiny(2)
blue(2)
flower(1)
candy(1)'''.split('\n')
这是你需要的吗?
def parse_file(open_file):
result = []
for line in open_file:
indent_level = len(line) - len(line.lstrip())
if indent_level == 0:
levels = ['','','']
item = line.lstrip().split('(', 1)[0]
levels[indent_level - 1] = item
if indent_level == 3:
result.append('.'.join(levels))
return result
data1 = set(parse_file(file1))
data2 = set(parse_file(file2))
differences = [
('common elements', data1 & data2),
('missing from file2', data1 - data2),
('missing from file1', data2 - data1) ]
要看到差异:
for desc, items in differences:
print desc
print
for item in items:
print '\t' + item
print
版画
common elements
giant.red.brick
tiny.blue.candy
tiny.blue.flower
missing from file2
tiny.green.dot
giant.red.apple
missing from file1
giant.red.tomato
内容总结
以上是互联网集市为您收集整理的python – 如何比较集群?全部内容,希望文章能够帮你解决python – 如何比较集群?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。