Skip to main content
 首页 » 编程设计

python之合并文本中的重复字符串

2025年05月04日24lautakyan007

我有一个这种格式的词频列表:

3 yaz 
1 yazlik 
5 zemin 
3 zemine 
1 zeminde 
2 zeminler 

zemine 和 zeminde 是不同的字符串,但具有相同的根 zemin

我想像这样合并列表:

4 yaz 
11 zemin 

我如何使用 bash 或 python 做到这一点?

请您参考如下方法:

bash (4.0+) 的解决方案甚至可以处理未排序的列表:

$ cat script.sh  
#!/bin/bash 
declare -A roots # declare roots as an associative array (bash 4.0+) 
while read n word; do 
    unset shortest longest 
    # check if the element (or its root) is already registered 
    for root in "${!roots[@]}"; do 
        if [[ "$root" =~ ^$word ]]; then 
            shortest=$word 
            longest=$root 
        elif [[ "$word" =~ ^$root ]]; then 
            shortest=$root 
            longest=$word 
        fi 
    done 
    # if registered, check if it must be replaced for a shorter one (its root) 
    if [ "$longest" ] && [ "${roots[$longest]}" ]; then 
        tmp_n=${roots["$longest"]} 
        unset roots["$longest"] 
        roots["$shortest"]=$tmp_n  
    fi 
    # register or update the element 
    let roots[${shortest:-$word}]+=$n 
done < list 
 
# print the result 
for root in "${!roots[@]}"; do 
    echo "${roots[$root]} $root" 
done 

例子

$ cat list 
3 yaz 
1 yazlik 
1 zeminde 
5 zemin 
3 zemine 
2 zeminler 
 
$ ./script.sh  
4 yaz 
11 zemin