def sort_set_by_list(s, l, keep_duplicates=True):
    """
    Convert the set `s` into a list ordered by a list `l`.
    
    Elements in `s` which are not in `l` are omitted.
    If ``keep_duplicates==True``, keep duplicate occurrences in `l` in the result; otherwise, only keep the first occurrence.
    """
    if keep_duplicates:
        return [e for e in l if e in s]
    else:
        res=[]
        s=s.copy()
        for e in l:
            if e in s:
                res.append(e)
                s.remove(e)
        return res