【发布时间】:2019-01-16 20:58:28
【问题描述】:
我想将一行与前一行进行比较,而不在内存中存储任何内容(没有字典)。
样本数据:
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)
我知道我会在数组上使用enumerate 或dict.iteritems,但我不能使用sys.stdin。
期望的输出:
a 2
file 7
for 1
has 1
is 2
lines 1
small 3
test 3
this 3
two 1
【问题讨论】:
-
可以重复2次以上吗?
-
您需要将
previous_word分配给某事 -
嗨,我编辑了我的问题,所以我正在寻找可能适用于超过 2 次重复但它们将彼此相邻的东西。我知道 previous_word 和 count1, count2 没有分配;我不确定我是否可以使这种方法起作用?
-
所以是 Python 还是 bash?顺便说一句,你可以在
sys.stdin上使用enumerate。 -
“我不确定我是否可以使这种方法工作” - 所以,试一试 - 将你的伪代码转换为代码 - 然后发布你的代码,实际输出和调试您尝试过;这将构成MCVE 的基础,这是关于 SO 的一个好问题