【问题标题】:How to remove lines from a file that are sub-strings of other lines [closed]如何从文件中删除作为其他行子字符串的行[关闭]
【发布时间】:2016-10-31 22:57:26
【问题描述】:

这是一个文件,其中许多行是其他行的子字符串。我如何过滤它以仅包含每行的最长版本?

buffer not
buffer not available
code 000001
error pxa_no_shared_memory
error pxa_no_shared_memory occurred
error pxa_no_shared_memory occurred short
error pxa_no_shared_memory occurred short dump
failed return
failed return code
failed return code 000001
for pxa
for pxa buffer
for pxa buffer not
for pxa buffer not available
initialization runt
initialization runt failed
initialization runt failed return
initialization runt failed return code
initialization runt failed return code 000001
memory for
memory for pxa
memory for pxa buffer
memory for pxa buffer not
memory for pxa buffer not available
not available
occurred short
occurred short dump

如果短短语出现在较长的短语中,例如“buffer not”也出现在“buffer not available”和“memory for pxa buffer not available”中,我想保留“memory for pxa buffer not available”。

输出应该是一个包含所有最长错误消息的文本文件。 像这样:

error pxa_no_shared_memory occurred short dump
initialization runt failed return code 000001
memory for pxa buffer not available

【问题讨论】:

  • 我不明白为什么人们如此频繁地对问题投反对票。这对我来说似乎是一个有趣的问题。
  • 需要什么计算复杂度?
  • 不需要计算复杂度。谢谢
  • @intrepidhero 它被否决了,因为它根本没有表现出任何努力。这只是一个需求转储,要求 Stack Overflow 为他们做他们的工作。
  • @S.L.Barth 啊!我每天都学到一些东西。谢谢。

标签: python string for-loop


【解决方案1】:

不确定效率,但是:

with open('lines.txt') as f:
    original = f.read().splitlines()
    results = set(original)
    for o in original:
        for r in set(results):
            if o != r:
                try:
                    if o in r:
                        results.remove(o)
                    elif r in o:
                        results.remove(r)
                except KeyError:
                    pass

print('\n'.join(results))

【讨论】:

    【解决方案2】:

    这个怎么样:

    phrases = '''buffer not
    buffer not available
    code 000001
    error pxa_no_shared_memory
    error pxa_no_shared_memory occurred
    error pxa_no_shared_memory occurred short
    error pxa_no_shared_memory occurred short dump
    failed return
    failed return code
    failed return code 000001
    for pxa
    for pxa buffer
    for pxa buffer not
    for pxa buffer not available
    initialization runt
    initialization runt failed
    initialization runt failed return
    initialization runt failed return code
    initialization runt failed return code 000001
    memory for
    memory for pxa
    memory for pxa buffer
    memory for pxa buffer not
    memory for pxa buffer not available
    not available
    occurred short
    occurred short dump'''.split("\n")
    
    """ We want to find only the longest versions of each line """
    
    results = []
    for phrase in phrases:
        found = -1
        # check to see if there is a version of this phrase we've encountered already
        for i, r in enumerate(results):
            # if our new phrase is longer then replace the existing version
            if phrase.startswith(r) and len(phrase) > len(r):
                found = i
                break
            # if the existing version is longer than do nothing
            elif r.startswith(phrase):
                found = -2
                break
        if found == -2:
            continue
        elif found > -1:
            results[found] = phrase
        else:
            # otherwise it must be a new phrase
            results.append(phrase)
    

    不是很优雅,但可以完成工作。

    【讨论】:

      猜你喜欢
      • 2016-06-04
      • 2012-08-31
      • 2021-11-10
      • 2021-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多