0

パイソン初心者です。

私は現在差分を扱っています。私はgoogle python libraryでそれらを生成しています。

差分結果がどのように見えるかのサンプルを次に示します。

[(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 = 追加)。2 番目の要素は、テキストのスラブから追加または削除されるデータです。

これらの diff の結果を要約して、読者が 30 文字ほどしか変更されていないテキストのスラブ全体を読む必要なく、数行を読むだけで変更の要点を理解できるようにします。

これに向けた私の最初のステップは、タプルを文字の長さでランク付けし、上位 3 つの大きな変更を表示することです (元の順序で、両側に少し変更されていないテキストがあります)。

タプルを文字の長さでランク付けし、最も長い 3 つを取得して、順序が元のようになるように並べ替えるにはどうすればよいと思いますか?

理想的には、結果は次のようになります (上記の例を使用)。

...ただ書き出す必要があります... ...入力しました...

4

1 に答える 1

1
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]
于 2012-07-18T15:50:39.113 に答える