对于使用上一项循环遍历标准输入



我想将一行与前一行进行比较,而不在内存中存储任何内容(没有字典)。

示例数据:

a   2
file    1
file    2
file    4
for 1
has 1
is  2
lines   1
small   1
small   2
test    1
test    2
this    1
this    2
two 1

伪代码:

for line in sys.stdin:
    word, count = line.split()
    if word == previous_word:
        print(word, count1+count2)

我知道我会在数组上使用enumeratedict.iteritems,但我不能sys.stdin.

期望输出:

a   2
file    7
for 1
has 1
is  2
lines   1
small   3
test    3
this    3
two 1

基本逻辑是跟踪前一个单词。 如果当前单词匹配,则累积计数。 如果没有,请打印上一个单词及其计数,然后重新开始。 有一些特殊的代码来处理第一次和最后一次迭代。

stdin_data = [
    "a   2",
    "file    1",
    "file    2",
    "file    4",
    "for 1",
    "has 1",
    "is  2",
    "lines   1",
    "small   1",
    "small   2",
    "test    1",
    "test    2",
    "this    1",
    "this    2",
    "two 1",
]  
previous_word = ""
word_ct = 0
for line in stdin_data:
    word, count = line.split()
    if word == previous_word:
        word_ct += int(count)
    else:
        if previous_word != "":
            print(previous_word, word_ct)
        previous_word = word
        word_ct = int(count)
# Print the final word and count
print(previous_word, word_ct)

输出:

a 2
file 7
for 1
has 1
is 2
lines 1
small 3
test 3
this 3
two 1

你的代码快到了。虽然不想将整个事情存储在内存中是值得称赞的,但您必须存储上一行的累积组件:

prev_word, prev_count = '', 0
for line in sys.stdin:
    word, count = line.split()
    count = int(count)
    if word == prev_word:
        prev_count += count
    elif prev_count:
        print(prev_word, prev_count)
        prev_word, prev_count = word, count

我想将一行与前一行进行比较,而无需在内存中存储任何内容(没有字典)。

为了能够用相似的单词汇总所有先前行的计数,您需要保持某种状态。

通常这份工作适合awk。您可以考虑此命令:

awk '{a[$1] += $2} p && p != $1{print p, a[p]; delete a[p]} {p = $1} 
END { print p, a[p] }' file
a 2
file 7
for 1
has 1
is 2
lines 1
small 3
test 3
this 3
two 1

使用delete,此解决方案不会将整个文件存储在内存中。状态仅在处理具有相同第一个单词的行的持续时间内保持。

哎呀参考资料:

  • 有效的 AWK 编程
  • awk 教程

最新更新