Python:对Linux软件清单,按ASCII升序排序并排除重复项

本人已积累并正在维护一个Linux软件的清单,该清单以.txt格式保存,文件名为 linux_app_list.txt ,内容如下。

bat
alacritty
firefox
vivaldi
ripgrep
zsh-completions
zsh-autosuggestions
zsh-syntax-highlighting
zsh-theme-powerlevel10k
manjaro-asian-input-support-fcitx5
audacious
catfish
okular
neofetch
fcitx5-anthy
fcitx5-chinese-addons
fcitx5-gtk
fcitx5-qt
fcitx5-rime
ffmpeg
clang
screenfetch
lsblk
ipython
rsync
unzip
which
hwinfo
smartmontools
gthumb
geany
darktable
goldendict
krita
shotcut
supertuxkart
zettlr
synaptic
firefox
cmatrix
mcomix
emacs
filezilla
libreoffice
dupeguru
fdisk
mount
pandoc
kdevelop
virtualbox
dolphin
fillzilla
kdeconnect
yakuake
anthy
python
ripgrep

但这个清单是从文件末尾不断增添软件名,因而是乱序的,而且可能存在重复项。那么,对这个清单进行一定的排序并排除重复项,就是一个合理的需求。

现在,已设计了一份Python代码 sort_linux_app_list.py ,来实现上述需求。代码如下:

# open and read app list.
app_list_txt = "linux_app_list.txt"
with open(app_list_txt, 'r') as f:
    lines = f.readlines()
# sort list, etc.
app_list = [app.strip() for app in lines]
app_list = list(set(app_list))
app_list.sort()
print("app list length:", len(app_list))
print(app_list)
# save sorted list.
app_list_txt_sorted = "linux_app_list_sorted.txt"
with open(app_list_txt_sorted, 'w') as f:
    for app in app_list:
        f.write(app + '\n')
# search app in this list.
while True:
    app_search = input("search(q to quit): ")
    if app_search == 'q':
        print("Quit.")
        break
    if app_search in app_list:
        print(f"{app_search} is in list.")
    else:
        print(f"{app_search} is not in list.")

代码逻辑比较简单,用到的技术也不太复杂。

该份代码将排序和去重后的软件列表保存到了一个新文件 linux_app_list_sorted.txt 中,方便查看和处理。

此外,还提供了检索功能。如果有一个软件在列表中查不到,而又需要加到清单中,那么,可以手动加到清单文件中。没有设计自动添加代码,因为设计起来稍显麻烦,可能需要向原文件写入新的一行,甚至需要重新读取之。

终端运行结果如下:

app list length: 84
['alacritty', 'anthy', 'ark', 'audacious', 'bat', 'catfish', 'clang', 'cloc', 'cmatrix', 'curl', 'darktable', 'dia', 'dolphin', 'dupeguru', 'emacs', 'exa', 'fcitx5-anthy', 'fcitx5-chinese-addons', 'fcitx5-gtk', 'fcitx5-qt', 'fcitx5-rime', 'fdisk', 'ffmpeg', 'file', 'filezilla', 'fillzilla', 'firefox', 'gcc', 'geany', 'gimp', 'git', 'goldendict', 'grep', 'gthumb', 'htop', 'hwinfo', 'ipython', 'kate', 'kdeconnect', 'kdevelop', 'krita', 'less', 'libreoffice', 'lsblk', 'lshw', 'lsof', 'lua', 'make', 'manjaro-asian-input-support-fcitx5', 'mcomix', 'meld', 'mount', 'mpv', 'nano', 'ncdu', 'neofetch', 'nmap', 'okular', 'pandoc', 'python', 'ripgrep', 'rsync', 'rust', 'screenfetch', 'shotcut', 'smartmontools', 'supertuxkart', 'synaptic', 'tar', 'tree', 'unzip', 'virtualbox', 'vivaldi', 'vlc', 'wget', 'which', 'yakuake', 'zettlr', 'zip', 'zsh', 'zsh-autosuggestions', 'zsh-completions', 'zsh-syntax-highlighting', 'zsh-theme-powerlevel10k']
search(q to quit): zsh
zsh is in list.
search(q to quit): q
Quit.

新清单的文件内容如下:

alacritty
anthy
audacious
catfish
clang
cmatrix
darktable
dolphin
dupeguru
emacs
fcitx5-anthy
fcitx5-chinese-addons
fcitx5-gtk
fcitx5-qt
fcitx5-rime
fdisk
ffmpeg
filezilla
fillzilla
firefox
geany
goldendict
gthumb
hwinfo
ipython
kdeconnect
kdevelop
krita
libreoffice
lsblk
manjaro-asian-input-support-fcitx5
mcomix
mount
neofetch
okular
pandoc
python
ripgrep
rsync
screenfetch
shotcut
smartmontools
supertuxkart
synaptic
unzip
virtualbox
vivaldi
which
yakuake
zettlr