python - 合并/丢弃重叠的单词

标签 python ruby perl bash

我想合并相似的字符串(单词)(字符串在其他字符串中)。

 word
 wor 
 words
 wormhole
 hole    

会:

words
wormhole  

由于 wor 与以下内容重叠:wordwordswormhole -wor被丢弃;
word 重叠于:words - word 被丢弃;
hole 重叠于:wormhole - hole 被丢弃;
但是 wordswormhole 不重叠 - 所以它们保持不变。
我怎样才能做到这一点?

编辑
我的解决方案是:

while read a
do  
   grep $a FILE | 
   awk 'length > m { m = length; a = $0 } END { print a }'
done < FILE | 
sort -u

但我不知道这是否会导致大型数据集出现问题。

最佳答案

在 ruby 中:

list = %w[word wor words wormhole]

list.uniq
.tap{|a| a.reverse_each{|e| a.delete(e) if (a - [e]).any?{|x| x.include?(e)}}}

关于python - 合并/丢弃重叠的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17013024/

相关文章:

python - 为什么这是 python 中的无限循环?

ruby-on-rails - 优雅的 ruby​​ 函数,需要帮助清理它,让它看起来像 ruby

Javascript 快速数组声明

ruby-on-rails - Rails HTML 请求渲染 JSON

perl - 如何为非登录用户设置单独的 Perl 模块?

perl如何找到http请求完成的时间

python - Django 如何使用模板标签拆分字符串

php - mySql 使用键连接表

python - 如何使用 HTML 实体将 Unicode 编码为 ASCII

ruby - 让 ruby​​ 对象响应任意消息?