【问题标题】:Compare Line to Next Line比较行和下一行
【发布时间】:2014-11-17 11:49:26
【问题描述】:

我有一个包含一堆项目的文本文件:

一个

两个

三个

三个

四个

五个

五个

我希望它吐出三和五,因为它们不止一次出现。

【问题讨论】:

    标签: if-statement for-loop compare duplicates python-2.5


    【解决方案1】:

    在 python 中它看起来像这样:

    lines = {}
    with open( "file.txt", "r" ) as src:
      for line in ins.readlines():
          lines[line] = lines.get(line, 0) + 1
    
    for line_key in lines.keys():
       if lines.get(line_key, 0 ) > 0:
          print line_key
    

    【讨论】:

      猜你喜欢
      • 2021-03-29
      • 2015-08-20
      • 1970-01-01
      • 1970-01-01
      • 2014-11-01
      • 2020-04-30
      • 2021-09-20
      • 2014-05-15
      相关资源
      最近更新 更多