Use sort() or search()?
<p>If I have a file such as below, what would be the best method to print each line that contains the first field in common (ie. print each line that field[0] = 'a', each line that field[0] = 'b')?  Should I use sort(), or populate a dictionary with value as a list, or utilize a class to classify each field, or use search()?  Perhaps I am overcomplicating a simple solution.</p>

<p>The file:</p>

<pre><code>a 2012 fff

b 2012 xxx

a 2011 fff

a 2011 vvv

b 2010 sss
</code></pre>

<p>Regular sort:</p>

<pre><code>with open('test.txt','r') as file:
    for line in file:
        field0,field1,field2=line.split()
        field0.sort() # ???
</code></pre>

<p>OR</p>

<pre><code>class MyClass:
    def __init__(self,field0,field1,field2):
        self.field0=field0
        self.field1=field1
        self.field2=field2
    def grabField():
        for i in MyClass.field0:
            print MyClass.field0,MyClass.field1,MyClass.field2

with open('test.txt','r') as file:
    for line in file:
        field0,field1,field2=line.split()
        MyClass(field0,field1,field2)
grabField()
</code></pre>

<p>I have determined the following code to populate dictionary and sort values:</p>

<pre><code>from collections import defaultdict
res = defaultdict(list)
with open('test.txt','r') as file:
    for line in file:
        field0, field1, field2 = line.split()
        res[field0] += [field1, field2]

for i in res.keys():
    print i, sorted(res[i], key=lambda x: x[0])
</code></pre>

<p>However, this yields "a [2011, 2011, 2012, fff, fff, vvv]" and I want output to be in the exact same format as the input (but sorted):</p>

<pre><code>a 2011 fff

a 2011 vvv

a 2012 fff
</code></pre>

<p>I can't just use sort on the original input since I will be referencing multiple times and need to store in memory.  Should value for each line be stored in a list (ie. dictionary value is list of lists)?</p>
