sort a list by prefered order
<p>is it possible to sort through a list and do something depending on your preferred order or pre determined choice?</p>

<p>so i have a list of files containing definitions, if i re.search the string and return a match, i would like to only print the string + highest definition</p>

<p>i started to explore the below idea but i cannot seem to get it to print only the highest -currently it prints all 3</p>

<pre><code>#!/usr/bin/python3
import re
definitions = ['1080p', '720p', 'SD'] # order (highest to lowest)
some_files = ['movie_a_1080p.mp4', 'movie_b_720p.mp4','movie_c_SD.mp4'] 

for each in some_files:
    for defs in definitions:
        match = re.search(defs, each, re.M | re.I)
        if match:
            if match.group() == '1080p':
                print('1080p', each)
                break
            else:
                if match.group() == '720p':
                    print('720p', each)
                    break
                else:
                if match.group() == 'SD':
                    print('SD', each)
                    break
</code></pre>

<p>any help would be awesome</p>

<p>Since your <code>definitions</code> is in order, you can just traverse the lists, and return the file when a match is found.   </p>

<pre><code>def best_quality(l):
    definitions = ['1080p', '720p', 'SD'] # order (highest to lowest)
    for definition in definitions:
        for file in some_files:
            if definition in file: return file

some_files = ['movie_c_SD.mp4', 'movie_a_1080p.mp4', 'movie_b_720p.mp4']
print best_quality(some_files)
&gt;&gt;&gt; movie_a_1080p.mp4       
</code></pre>

python python-2.7 python-3.x