python – 清除自己的字符串中包含的字符串列表

python – 清除自己的字符串中包含的字符串列表,第1张

概述我有一个文本文件lists.txt,如下所示: HI family what are u doing ?HI Familywhat areChannel 5 is very cheapChannel 5 isChannel 5 is veryPokemonThe best Pokemon is Pikachu 我想清理它,删除任何完全包含在其他行中的行.也就是说,我想要这样的东西: 我有一个文本文件Lists.txt,如下所示:

HI family what are u doing ?HI Familywhat areChannel 5 is very cheapChannel 5 isChannel 5 is veryPokemonThe best Pokemon is pikachu

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

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

我已经尝试计算大量的字符串,然后将其与grep进行比较,在大的results.txt上找到sorts 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.txtHI family what are u doin?HI family what areChannel 5 is very cheapChannel 5 isChannel 5 is veryPokemonThe 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.txtHI family what are u doin?Channel 5 is very cheapThe best Pokemon is pikachu$
总结

以上是内存溢出为你收集整理的python – 清除自己的字符串中包含的字符串列表全部内容,希望文章能够帮你解决python – 清除自己的字符串中包含的字符串列表所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1193120.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存