Numerically order a list based on a pattern
<p>I have a list of file names in the form:</p>

<pre><code>['comm_1_1.txt', 'comm_1_10.txt', 'comm_1_11.txt', 'comm_1_4.txt', 'comm_1_5.txt', 'comm_1_6.txt']
</code></pre>

<p>I wonder how to sort this list numerically to obtain the output:</p>

<pre><code>['comm_1_1.txt', 'comm_1_4.txt', 'comm_1_5.txt', 'comm_1_6.txt', 'comm_1_10.txt', 'comm_1_11.txt']
</code></pre>

<p>I don't really think its a best answer, but you can try it out.     </p>

<pre><code>l = ['comm_1_1.txt', 'comm_1_10.txt', 'comm_1_11.txt', 'comm_1_4.txt', 'comm_1_5.txt', 'comm_1_6.txt'] 

d = {} 

for i in l:
    filen = i.split('.')
    key = filen[0].split('_')
    d[int(key[2])] = i

for key in sorted(d):
       print(d[key])
</code></pre>

python sorting