Python find and replace last appearance in list
<p>In Python, I have a list of list</p>

<pre><code>list3 = ['PA0', 'PA1']
list2 = ['PB0', 'PB1']
list1 = ['PC0', 'PC1', 'PC2']

[(list1[i], list2[j], list3[k]) for i in xrange(len(list1)) for j in xrange(len(list2)) for k in xrange(len(list3))]

#Result
[('PC0', 'PB0', 'PA0'), 
('PC0', 'PB0', 'PA1'), 
('PC0', 'PB1', 'PA0'), 
('PC0', 'PB1', 'PA1'), 
('PC1', 'PB0', 'PA0'), 
('PC1', 'PB0', 'PA1'), 
('PC1', 'PB1', 'PA0'), 
('PC1', 'PB1', 'PA1'), 
('PC2', 'PB0', 'PA0'), 
('PC2', 'PB0', 'PA1'), 
('PC2', 'PB1', 'PA0'), 
('PC2', 'PB1', 'PA1')]
</code></pre>

<p>How can I find the <strong>last appearance</strong> and add <strong>E</strong> as suffix</p>

<pre>
[('PC0', 'PB0', 'PA0'), 
 ('PC0', 'PB0', 'PA1'), 
 ('PC0', 'PB1', 'PA0'), 
 (<b>'PC0E'</b>, 'PB1', 'PA1'), 
 ('PC1', 'PB0', 'PA0'), 
 ('PC1', 'PB0', 'PA1'), 
 ('PC1', 'PB1', 'PA0'), 
 (<b>'PC1E'</b>, 'PB1', 'PA1'), 
 ('PC2', 'PB0', 'PA0'), 
 ('PC2', <b>'PB0E'</b>, 'PA1'), 
 ('PC2', 'PB1', <b>'PA0E'</b>), 
 (<b>'PC2E'</b>, <b>'PB1E'</b>, <b>'PA1E'</b>)]
</pre>

<p>If you are not looking for lightning speed here, you could do the following:</p>

<ol>
<li>Flatten the list using <a href="http://stackoverflow.com/a/952952/2988730">http://stackoverflow.com/a/952952/2988730</a></li>
<li>Find the unique elements</li>
<li>Find the index of the last occurrence of each unique element (by reversing the list)</li>
<li>Update the element</li>
<li>Reshape the flattened list back using <a href="http://stackoverflow.com/a/10124783/2988730">http://stackoverflow.com/a/10124783/2988730</a></li>
</ol>

<p>Here is a sample implementation:</p>

<pre><code># 1
flat = list(reversed([x for group in mylist for x in group]))
# 2
uniq = set(flat)
# 3, 4
for x in uniq:
    flat[flat.index(x)] += 'E'
# 5
mylist = list(zip(*[reversed(flat)]*3))
</code></pre>

<p>Result:</p>

<pre><code>[('PC0', 'PB0', 'PA0'),
 ('PC0', 'PB0', 'PA1'),
 ('PC0', 'PB1', 'PA0'),
 ('PC0E', 'PB1', 'PA1'),
 ('PC1', 'PB0', 'PA0'),
 ('PC1', 'PB0', 'PA1'),
 ('PC1', 'PB1', 'PA0'),
 ('PC1E', 'PB1', 'PA1'),
 ('PC2', 'PB0', 'PA0'),
 ('PC2', 'PB0E', 'PA1'),
 ('PC2', 'PB1', 'PA0E'),
 ('PC2E', 'PB1E', 'PA1E')]
</code></pre>

python list