Efficiently insert multiple elements in a list (or another data structure) keeping their order

2024/9/29 19:20:44

I have a list of items that should be inserted in a list-like data structure one after the other, and I have the indexes at which each item should be inserted. For example:

items = ['itemX', 'itemY', 'itemZ']
indexes = [0, 0, 1]

The expected result is to have a list like this: result = ['itemY', 'itemZ', 'itemX'].

I'm able to get this result with this simple approach:

result = []
for index, item in zip(indexes, items):result.insert(index, item)

However, this is a very slow approach once lists become huge (the complexity is O(n^2)). Is there any (relatively simple to implement) way to improve my basic approach? I guess I have to look at other data structures while I insert elements and finally transform that data structure into my result list. Are trees a good option? Insert could be done maybe in O(log(n)) (instead of O(n)), but which specific tree-like structure should I use?

Or maybe something good can be achieved by just looking at all the indexes together (instead of using them one by one).

This is probably the worst case for my slow approach (always insert items at the beginning of the list):

n = 10**6 # some large number
items = list(range(n))
indexes = [0] * n
Answer

Here's python code for a treap with a size decoration that allows insertion at specific indexes, and reordering of whole contiguous sections. It was adapted from C++ code, Kimiyuki Onaka's solution to the Hackerrank problem, "Give Me the Order." (I cannot guarantee that this adaptation is bug free -- a copy of the original code is available in the description of this question.)

import randomclass Treap:def __init__(self, value=None):self.value = valueself.key = random.random()self.size = 1self.left = Noneself.right = Nonedef size(t):return t.size if t else 0def update(t):if t:t.size = 1 + size(t.left) + size(t.right)return tdef merge(a, b):if not a:return bif not b:return aif a.key > b.key:a.right = merge(a.right, b)return update(a)else:b.left = merge(a, b.left)return update(b)def split(t, i):if not t:return None, Noneif i <= size(t.left):u, t.left = split(t.left, i)return u, update(t)else:t.right, u = split(t.right, i - size(t.left) - 1)return update(t), udef insert(t, i, value):left, right = split(t, i)u = Treap(value)return merge(merge(left, u), right)def inorder(treap):if not treap:returnif treap.left:inorder(treap.left)print(treap.value)if treap.right:inorder(treap.right)

Output:

lst = ['itemX', 'itemY', 'itemZ']
idxs = [0, 0, 1]t = Nonefor i in range(len(lst)):t = insert(t, idxs[i], lst[i])inorder(t)"""
itemY
itemZ
itemX
"""
https://en.xdnf.cn/q/71173.html

Related Q&A

matplotlib versions =3 does not include a find()

I am running a very simple Python script: from tftb.generators import amgauss, fmlinI get this error: C:\Users\Anaconda3\envs\tf_gpu\lib\site-packages\tftb-0.0.1-py3.6.egg\tftb\processing\affine.py in …

How to fix Field defines a relation with the model auth.User, which has been swapped out

I am trying to change my user model to a custom one. I do not mind dropping my database and just using a new one, but when I try it to run makemigrations i get this errorbookings.Session.session_client…

Generate and parse Python code from C# application

I need to generate Python code to be more specific IronPyton. I also need to be able to parse the code and to load it into AST. I just started looking at some tools. I played with "Oslo" and …

An efficient way to calculate the mean of each column or row of non-zero elements

I have a numpy array for ratings given by users on movies. The rating is between 1 and 5, while 0 means that a user does not rate on a movie. I want to calculate the average rating of each movie, and t…

Selecting unique observations in a pandas data frame

I have a pandas data frame with a column uniqueid. I would like to remove all duplicates from the data frame based on this column, such that all remaining observations are unique.

GEdit/Python execution plugin?

Im just starting out learning python with GEdit plus various plugins as my IDE.Visual Studio/F# has a feature which permits the highlighting on a piece of text in the code window which then, on a keyp…

autoclass and instance attributes

According to the sphinx documentation, the .. autoattribute directive should be able to document instance attributes. However, if I do::.. currentmodule:: xml.etree.ElementTree.. autoclass:: ElementTre…

python sqlite3 update not updating

Question: Why is this sqlite3 statement not updating the record?Info:cur.execute(UPDATE workunits SET Completed=1 AND Returns=(?) WHERE PID=(?) AND Args=(?),(pickle.dumps(Ret),PID,Args))Im using py…

Unable to reinstall PyTables for Python 2.7

I am installing Python 2.7 in addition to 2.7. When installing PyTables again for 2.7, I get this error -Found numpy 1.5.1 package installed. .. ERROR:: Could not find a local HDF5 installation. You ma…

How can I invoke a thread multiple times in Python?

Im sorry if it is a stupid question. I am trying to use a number of classes of multi-threading to finish different jobs, which involves invoking these multi-threadings at different times for many times…