gpt4 book ai didi

python - 包含在自己的字符串中的干净字符串列表

转载 作者:太空宇宙 更新时间:2023-11-03 13:17:49 25 4
gpt4 key购买 nike

我有一个文本文件 lists.txt,它看起来像这样:

HI family what are u doing ?
HI Family
what are
Channel 5 is very cheap
Channel 5 is
Channel 5 is very
Pokemon
The best Pokemon is Pikachu

我想清理它,删除完全包含在其他行中的所有行。也就是说,我想要这样的东西:

HI family, what are u doing ?
The best Pokemon is Pikachu
Channel 5 is very cheap

我尝试对较大的字符串进行计数,然后将其与 grep 进行比较,在较大的 results.txt 上找到排序 results.txt,但收效甚微。

最佳答案

如果我对您的问题的理解正确,您想要获取一个字符串列表并从中删除作为列表中其他字符串的子字符串的所有字符串。

在伪代码中,

outer: for string s in l
for string s2 in l
if s substringOf s2
continue outer
print s

即对每个字符串循环一次字符串,如果其内部循环中的任何测试匹配,则取消外部循环的每次运行。

这是该算法在 bash 中的实现。请注意,正在通过重定向运算符 list.txt 读取文件 ( <)在代码中两次,一次用于外循环,一次用于内循环。

(我还清理了您的示例,其中有很多拼写错误。)

$ cat list.txt
HI family what are u doin?
HI family what are
Channel 5 is very cheap
Channel 5 is
Channel 5 is very
Pokemon
The best Pokemon is Pikachu
$ while read line; do while read line2; do [[ $line2 != $line ]] && [[ $line2 == *$line* ]] && continue 2; done <list.txt; echo "$line"; done <list.txt
HI family what are u doin?
Channel 5 is very cheap
The best Pokemon is Pikachu
$

关于python - 包含在自己的字符串中的干净字符串列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23916585/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com