Padding zeros into list at specific positions
<p>Given two lists of different length, but with mostly similar (or overlapping) values such as:</p>

<pre><code>ls_1 = [7, 26, 26, 55, 69, 71, 73, 80, 121, 124, 126, 127, 
        131, 133, 144, 153, 153, 159, 160, 210, 219, 221, 
        235, 235, 241, 243, 289, 299, 300, 309, 327, 327, 
        328, 391, 419, 421, 423, 433]

ls_2 = [7, 28, 28, 60, 69, 81, 121, 124, 125, 127, 131, 133, 
       144, 153, 153, 159, 160, 210, 219, 221, 235, 235, 241, 
       243, 327, 327, 330, 391, 419, 421, 423, 433]
</code></pre>

<p>Is there a an easy way to pad in the 'missing' values in list 2 with zeros so that the lists match in length?</p>

<p>EDIT: I'm trying to find a way of making the lists have the same exact length by placing zeros where they are <em>most likely</em> to be found. Likelihood here is intended as as the measured distance between item[i] in list 1 and item[i] in list 2. </p>

<p><em>Note1</em>: I understand that the problem is no well defined. A useful answer would for instance point me at a method to compare items in lists and finding a likely threshold.</p>

<p><em>Note2</em>: Lists are always sorted, performance is not a big issue.</p>

<p>Example: insert two zero between the values 69 and 81, and some more other zeros between 330 and 391.</p>

<p>My approach so far has been calculating the difference between values and when the <code>difference &gt; some_treshold</code>, I'd do a <code>ls_2.insert</code>. </p>

<p>However this seems not to be the most foolproof method, since it requirers an arbitrary threshold. </p>

<pre><code>for i in range(len(ls_2)):
    distance = ls_2[i] - ls_1[i]
    if distance &gt; 3:
        ls_2.insert(i, 0)

print(len(ls_2), len(ls_1))  #double-checking, lengths are the same.
</code></pre>

<p>I'm asking this question specifically because it leads to another question, that is how to compare different sized lists of integers. What I have in mind is  that by doing the padding here described would enable me to implement more straightforwardly algorithms such as cos distance and euclidean distance. The very end goal is to pad a large number of lists and rank them by similarity to reference list of integers.</p>

<p>Let me know if all this makes sense or if there are more straightforward ways of going about it. (And if it'd be more appropriate to post two separate questions). </p>

<p>Go about it by adding 0s at the end of your <code>ls_2</code>. Not in between as you have pointed out. This reduces a lot of complexity.</p>

<p>Something like:</p>

<pre><code>ls_1_length = len(ls_1)
ls_2_length = len(ls_2)
length_diff = ls_1_length - ls_2_length
for index in length_diff:
    ls_2.append(0)
</code></pre>

<p>For the second part of your question. If you want to compare two lists of variable size why do you want to make these two lists the same size?</p>

<p>Hope this helps.</p>

python sorting padding