Unordered collection for mutable objects in Python
<p>The task I have at hand is to parse a large text (several 100K rows) file and accumulate some statistics based which will be then visualized in plots. Each row contains results of some prior analysis. </p>

<p>I wrote a custom class to define the objects that are to be accumulated. The class contains 2 string fields, 3 sets and 2 integer counters. As such there is an <code>__init__(self, name)</code> which initializes a new object with name and empty fields, and a method called <code>addRow()</code> which adds information into the object. The sets accumulate data to be associated with this object and the counters keep track of a couple of conditions. </p>

<p>My original idea was to iterate over the rows of the file and call a method like <code>parseRow()</code> in <code>main</code></p>

<pre><code>reader = csv.reader(f)
acc = {} # or set()
for row in reader: 
  parseRow(row,acc)
</code></pre>

<p>which would look something like:</p>

<pre><code>parseRow(row, acc):
  if row[id] is not in acc: # row[id] is the column where the object names/ids are 
    a = MyObj(row[id])
  else:
    a = acc.get(row[id]) # or equivalent
  a.addRow(...)
</code></pre>

<p>The issue here is that the accumulating collection <code>acc</code> cannot be a <code>set</code> since sets are apparently not indexable in Python. <strong>Edit:</strong> for clarification, by <em>indexable</em> I didn't mean getting the nth element but rather being able to <strong>retrieve a specific element</strong>. </p>

<p>One workaround would be to have a <code>dict</code> that has <code>{obj_name : obj}</code> mapping but it feels like an ugly solution. Considering the elegance of the language otherwise, I guess there is a better solution to this. It's surely not a particularly rare situation...</p>

<p>Any suggestions?</p>

<p>You could also try an <a href="https://pypi.python.org/pypi/ordered-set" rel="nofollow">ordered-set</a>. Which is a set and ordered.</p>

python python-3.x collections