How to sort a list based on another list with integers Python
<p>So, I'm generally new at programming, and I was just wondering if it was possible to sort a list based not on the positions of another list, but the actual integers itself?</p>

<pre><code>#Here is my list to be sorted
sent = ['Ho','Hi','Hi','Ho','Hi','Ho','!','Hi']

#The positions I will sort by
pos = [1, 2, 2, 1, 1, 1, 3]

#The output I want is
sortlst = ['Hi','Ho','Ho','Hi','Hi','Hi','Ho','!']
</code></pre>

<p>I have tried searching for methods to resolve this, but could not find any. List comprehensions and the like has been able to call elements in sent, but not by the actual integers of pos, rather the position.</p>

<pre><code>for i in pos:
    T.append(sent[i])
recreate = " ".join(T) 
print (recreate)
</code></pre>

<p>Running this segment will output:</p>

<pre><code>Ho Ho Ho Ho Ho Ho Ho Hi
</code></pre>

<p>Any help would be greatly appreciated!</p>

<p>I will make a few assumptions for my proposed solution.</p>

<ul>
<li><code>pos</code> has 7 elements but <code>sortlst</code> has 8. I assume this is a typo.</li>
<li>You associate each <em>unique</em> element of <code>sent</code> with an integer based on its sorted value. Note, though, that you sort '!' as <em>after</em> 'Ho', even though it is <em>before</em> in lexicographical order. I will use lexicographical order unless you propose an alternative.</li>
</ul>

<p>Therefore, <code>post = [1,2,3]</code> is telling me:</p>

<blockquote>
  <p>Give me an array with the first, second, and third unique sorted values of <code>sent</code>.</p>
</blockquote>

<p>With this definition in mind:</p>

<pre><code>&gt;&gt;&gt; sent = ['Ho','Hi','Hi','Ho','Hi','Ho','!','Hi']
&gt;&gt;&gt; pos = [1, 2, 2, 1, 1, 1, 3]
&gt;&gt;&gt; elements = sorted(set(sent))
&gt;&gt;&gt; elements
['!', 'Hi', 'Ho']
&gt;&gt;&gt; [elements[i - 1] for i in pos]
['!', 'Hi', 'Hi', '!', '!', '!', 'Ho']
</code></pre>

python