0

私はPythonに比較的慣れていないので、この質問を聞きたいと思います。

番号0〜9で作成された10000シーケンスの長さがあります。

目標:10個の異なる場所に10個の数字シーケンス(すでにその数を知っている)をランダムに挿入し、挿入する前にその場所を記録します。

したがって、関数は次のようになります。

def insertion( insertion_sequence_list, long_sequence ):
    #modifying the long sequence
    return inserted_long_sequence and inserted_positions

どうすればいいですか?私が直面している問題は、ランダムな位置にインサート1を作成するたびに、後の位置が変わることです。

例えば:

私は123456789123456789を長いシーケンスとして持っています

2番目の位置(129993456789123456789)に「999」を挿入すると。しかし、後で、シーケンス「888」を3番目の位置に挿入するときは、元の位置にしたいので、129993 * 888 *456789123456789にします。ただし、実際には、代わりに129 * 888 *993456789123456789になります。どうすればこれを修正できるのかわかりません。

重複の可能性がある場合はお知らせください。この質問が何に属するのかさえわかりません:\

すべてのコメント、意見、回答をありがとう!

4

3 に答える 3

2

後の位置のみが変更されるため、挿入操作を収集し、それらを並べ替えてから最新の位置を挿入すると、すべてが機能します。

insertion_ops = [(position, insertion) for ...]
for position, insertion in reversed(sorted(insertion_ops)):
    sequence[position:position] = insertion

または、挿入位置を負の位置、つまり端からのオフセットに変換することもできます。ただし、最初にそれらを並べ替える必要があります。

于 2012-06-22T15:55:57.887 に答える
2

これを行うには、場所で並べ替えて逆の順序で適用します。同点の場合、順序は重要ですか?次に、場所と順序ではなく、場所のみで並べ替えて、正しい順序で挿入されるようにします。たとえば、999@1を挿入してから888@1を挿入すると、両方の値で並べ替えると888 @ 1,999@1になります。

12345
18889992345

ただし、安定した並べ替えで場所のみを並べ替えると、999 @ 1,888@1になります。

12345
1999888345

コードは次のとおりです。

import random
import operator

# Easier to use a mutable list than an immutable string for insertion.
sequence = list('123456789123456789')
insertions = '999 888 777 666 555 444 333 222 111'.split()
locations = [random.randrange(len(sequence)) for i in xrange(10)]
modifications = zip(locations,insertions)
print modifications
# sort them by location.
# Since Python 2.2, sorts are guaranteed to be stable,
# so if you insert 999 into 1, then 222 into 1, this will keep them
# in the right order
modifications.sort(key=operator.itemgetter(0))
print modifications
# apply in reverse order
for i,seq in reversed(modifications):
    print 'insert {} into {}'.format(seq,i)
    # Here's where using a mutable list helps
    sequence[i:i] = list(seq)
    print ''.join(sequence)

結果:

[(11, '999'), (8, '888'), (7, '777'), (15, '666'), (12, '555'), (11, '444'), (0, '333'), (0, '222'), (15, '111')]
[(0, '333'), (0, '222'), (7, '777'), (8, '888'), (11, '999'), (11, '444'), (12, '555'), (15, '666'), (15, '111')]
insert 111 into 15
123456789123456111789
insert 666 into 15
123456789123456666111789
insert 555 into 12
123456789123555456666111789
insert 444 into 11
123456789124443555456666111789
insert 999 into 11
123456789129994443555456666111789
insert 888 into 8
123456788889129994443555456666111789
insert 777 into 7
123456777788889129994443555456666111789
insert 222 into 0
222123456777788889129994443555456666111789
insert 333 into 0
333222123456777788889129994443555456666111789
于 2012-06-22T16:20:54.997 に答える
1

あなたはどのinsertion_sequence_listように見えますか?これに沿ったものである場合:

[('999', 2),
 ('888', 3)]

次に、2番目の値に基づいて降順で並べ替える必要があります。

from operator import itemgetter

ins_seq_li.sort(key = itemgetter(1), reverse = True)

次に、そのリストから挿入を行う場合、最初に最大のインデックスで追加するので、以前の挿入で問題ないはずです。

于 2012-06-22T15:57:17.187 に答える