用python解析差异结果(元组列表)

用python解析差异结果(元组列表),python,diff,python-2.7,Python,Diff,Python 2.7,这里是Python初学者 我现在正在和Diff合作。我正在生成它们 下面是一个diff结果的示例: [(0, 'Ok. I just '), (-1, 'need to write '), (0, 'out a random bunch of text\nand then keep going. I'), (-1, ' just'), (0, " did an enter to see how that goes and all\nthe rest of it. I know

这里是Python初学者

我现在正在和Diff合作。我正在生成它们

下面是一个diff结果的示例:

[(0, 'Ok.  I just '),
 (-1, 'need to write '),
 (0, 'out a random bunch of text\nand then keep going.  I'),
 (-1, ' just'),
 (0,
  " did an enter to see how that goes and all\nthe rest of it.  I know I need.  Here's a skipped line.\n\nThen there is more and "),
 (-1, 't'),
 (0, 'hen there was the thing.')]
这是一个元组列表。每个元组中的第一个元素是运算符(0-无更改,-1=删除,1=添加)。第二个元素是从文本板中添加或删除的数据

我想总结一下这些不同的结果,这样读者就可以通过阅读几行文字而不必阅读可能只有30个左右变化的整段文字来了解变化的要点

我实现这一点的第一步是按字符长度对元组进行排序,然后显示前3个最大的变化(按其原始顺序,两边都有一点不变的文本)

你认为我应该如何按照字符长度对元组进行排序,获取三个最长的元组,然后重新排列它们,使其顺序与原始元组相同

理想情况下,结果如下所示(使用上面的示例):

…只需要写一个。。。 …我刚进去

input = [(0, 'Ok.  I just '), (-1, 'need to write '), (0, 'out a random bunch of text\nand then keep going.  I'), (-1, ' just'), (0, " did an enter to see how that goes and all\nthe rest of it.  I know I need.  Here's a skipped line.\n\nThen there is more and "), (-1, 't'), (0, 'hen there was the thing.')]

top_3 = [filtered_change[1] for filtered_change in sorted(sorted(enumerate(input), key=lambda change: len(change[1][1]), reverse=True)[:3])]
或者,一步一步:

indexed_changes = enumerate(input)
indexed_and_sorted_by_length = sorted(indexed_changes, key=lambda change: len(change[1][1]), reverse=True)
largest_3_indexed_changes = indexed_and_sorted_by_length[:3]
largest_3_indexed_sorted_by_index = sorted(largest_3_indexed_changes)
largest_3_changes_in_original_order = [indexed_change[1] for indexed_change in largest_3_indexed_sorted_by_index]

您能为您的输出提供示例吗?您尝试了什么?输出示例位于问题的末尾。坦白地说,我还没有尝试过任何东西,因为我还不知道从哪里开始。这完美地回答了这个问题,当我做其他类似的事情时,给了我很多东西要学习。同时也感谢您花时间一步一步地进行阐述。