Note that there are some explanatory texts on larger screens.

plurals
  1. POPython: update a list of tuples... fastest method
    text
    copied!<p>This question is in relation to another question asked here: <a href="https://stackoverflow.com/questions/1180240/best-way-to-sort-1m-records-in-python">Sorting 1M records</a></p> <p>I have since figured out the problem I was having with sorting. I was sorting items from a dictionary into a list every time I updated the data. I have since realized that a lot of the power of Python's sort resides in the fact that it sorts data more quickly that is already partially sorted. </p> <p>So, here is the question. Suppose I have the following as a sample set:</p> <pre><code>self.sorted_records = [(1, 1234567890), (20, 1245678903), (40, 1256789034), (70, 1278903456)] </code></pre> <p><code>t[1]</code> of each tuple in the list is a unique id. Now I want to update this list with the follwoing:</p> <pre><code>updated_records = {1245678903:45, 1278903456:76} </code></pre> <p>What is the fastest way for me to do so ending up with </p> <pre><code>self.sorted_records = [(1, 1234567890), (45, 1245678903), (40, 1256789034), (76, 1278903456)] </code></pre> <p>Currently I am doing something like this:</p> <pre><code>updated_keys = updated_records.keys() for i, record in enumerate(self.sorted_data): if record[1] in updated_keys: updated_keys.remove(record[1]) self.sorted_data[i] = (updated_records[record[1]], record[1]) </code></pre> <p>But I am sure there is a faster, more elegant solution out there.</p> <p>Any help?</p> <p><strong>* edit <em></strong> It turns out I used bad exaples for the ids since they end up in sorted order when I do my update. I am actually interested in t[0] being in sorted order. After I do the update I was intending on resorting with the updated data, but it looks like bisect might be the ticket to insert in sorted order. <strong></em> end edit *</strong></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload