当前位置: 代码迷 >> python >> 计算一个单词的出现次数
  详细解决方案

计算一个单词的出现次数

热度:94   发布时间:2023-06-19 09:13:43.0

我检查了类似的主题,但结果很差。

我有一个这样的文件:

S1_22   45317082    31  0   9   22  1543
S1_23   3859606 40  3   3   34  2111
S1_24   48088383    49  6   1   42  2400
S1_25   43387855    39  1   7   31  2425
S1_26   39016907    39  2   7   30  1977
S1_27   57612149    23  0   0   23  1843
S1_28   42505824    23  1   1   21  1092
S1_29   54856684    18  0   2   16  1018
S1_29   54856684    18  0   2   16  1018
S1_29   54856684    18  0   2   16  1018
S1_29   54856684    18  0   2   16  1018

我想在第一列中计算单词的出现次数,并根据写入输出文件的附加字段说明uniq如果count == 1则多次if count> 0

我制作了代码:

import csv
import collections

infile = 'Results'

names = collections.Counter()

with open(infile) as input_file:
    for row in csv.reader(input_file, delimiter='\t'):
        names[row[0]] += 1
    print names[row[0]],row[0]

但它不能正常工作

我不能把所有东西放到列表中,因为文件太大了

最后的print语句看起来不像你想要的。 由于它的缩进,它只执行一次。 它将打印S1_29,因为这是循环的最后一次迭代中row[0]的值。

你走在正确的轨道上。 而不是那个print语句,只需遍历计数器的键和值,并检查每个值是否大于或等于1。

如果您希望此代码有效,则应缩进print语句:

    names[row[0]] += 1
    print names[row[0]],row[0]

但你真正想要的是:

import csv
import collections

infile = 'Result'

names = collections.Counter()

with open(infile) as input_file:
    for row in csv.reader(input_file, delimiter='\t'):
        names[row[0]] += 1

for name, count in names.iteritems():
    print name, count

编辑:要显示行的其余部分,您可以使用第二个字典,如:

names = collections.Counter()
rows = {}

with open(infile) as input_file:
    for row in csv.reader(input_file, delimiter='\t'):
        rows[row[0]] = row
        names[row[0]] += 1

for name, count in names.iteritems():
    print rows[name], count
  相关解决方案