gpt4 book ai didi

python - 按文本顺序对列表中的句子进行排序

转载 作者:行者123 更新时间:2023-11-30 22:13:21 25 4
gpt4 key购买 nike

我用Python从文本中提取了一些句子。文本存储在字符串中,句子存储在列表中。以下是一些示例输入:

text = "This is a text. This is sentence 1. Here is sentence 2. And this is sentence 3."
extracted = ['Here is sentence 2.', 'This is a text']

现在我想根据文本中的时间顺序对extracted列表中的元素进行排序。这是我想要的输出:

ordered_result = ['This is a text', 'Here is sentence 2.']

有人知道该怎么做吗?
提前致谢。

最佳答案

直接按原始字符串中的位置对它们进行排序:

ordered_result = sorted(extracted, key=lambda x: text.index(x))

关于python - 按文本顺序对列表中的句子进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50813898/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com