对于这个非常基本的问题,我提前表示歉意,我知道到处都有关于这个问题的帖子,但我似乎无法在其他网页上得到所有的帮助。你知道吗
首先,我是python的初学者,很抱歉代码太模糊了。但我只想计算某个字符串在第2列中出现的次数,而第1列中的值保持不变。如果这个值改变,循环应该重新开始。这听起来很简单,但是python将我的文本文件作为字符串来读取(给我带来了strip和split等问题)让我很困惑。我似乎无法让这个代码工作。请有人帮帮这个遇险的家伙!你知道吗
输入:
6 ABMV
6 ABMV
6 FOOD
6 FOOD
6 IDLE
10 IDLE
10 ABMV
10 IDLE
代码:
#! /usr/bin/env python
from collections import Counter
outfile = open ("counts_outfile.txt", "w")
with open("test_counts.txt", "r") as infile:
lines = infile.readlines()
for i, item in enumerate(lines):
lines[i] = item.rstrip().split('\t')
last_chimp = lines[0][0]
behavior = lines[0][1]
nr_ABMV = 0
nr_FOOD = 0
nr_IDLE = 0
for lines in infile:
chimp = lines[0][0]
behavior = lines[0][1]
if chimp == last_chimp:
if behavior == "ABMV":
nr_ABMV += 1
elif behavior == "FOOD":
nr_FOOD += 1
elif behavior == "IDLE":
nr_IDLE += 1
else:
continue
else:
outline = "chimp_header %s\t%s\t%s\t%s" % (last_chimp, nr_ABMV, nr_FOOD, nr_IDLE)
outfile.write(outline)
last_chimp == lines[0][0]
nr_ABMV = 0
nr_FOOD = 0
nr_IDLE = 0
outfile.close()
事先谢谢你,你会帮我的,显然还有很多‘黑猩猩’(黑猩猩)很多!!你知道吗
谨致问候
下面是一个与您的代码非常相似的示例:
下面是另一个使用
Counter
和字典的示例:两个例子产生相同的结果:
我希望这能给你一些想法。你知道吗
相关问题 更多 >
编程相关推荐