问题描述
我想了解如何按特定字符串结尾对我的纯文本文件进行排序,在本例中为后缀“ly”:
aback
abaft
abandonedly
abashedly
abeam
abed
abhorrently
...
从该列表中提取和/或拆分所有以“ly”结尾的词,它应该类似于以下内容:
aback
abaft
abeam
abed
...
abandonedly
abashedly
abhorrently
注意:
- 新列表是否附加到旧列表无关紧要。
- 我希望我的列表按字母顺序排列,如果 可能。
我愿意使用编程语言、命令行或服务。事实是,根据我之前的在线研究,我无法确定哪种方法最有效或最简单。
解决方法
Python 中的解决方案:
<tr>
<td>
<div id="element-id-to-identify-on-collapse-control" class="collapse">
<p>This element would be hidden and the animation will run</p>
</div>
<td>
<tr>
来自with open("input.txt","r") as f_in:
lines = sorted(map(str.strip,f_in),key=lambda k: (k.endswith("ly"),k))
with open("output.txt","w") as f_out:
print("\n".join(lines),file=f_out)
:
input.txt
创建aback
abaft
abandonedly
abashedly
abeam
abed
abhorrently
:
output.txt