Remove max and min duplicate of a list of number
<p>I would like to remove ONLY the maximum and minimum duplicate of a list of
numbers. Example: (<strong>1 1 1</strong> 4 4 5 6 <strong>8 8 8</strong>) Result: (<strong>1</strong> 4 4 5 6
<strong>8</strong>)</p>

<p>How do I combine max() min() function with <code>list(set(x))</code>?</p>

<p>Or is there another way?</p>

<pre><code>s = raw_input("Please Input a series of numbers")
numbers = map(int, s.split())

numbers.remove(max(numbers))
numbers.remove(min(numbers))
average = sum(numbers)/float(len(numbers))
print average
</code></pre>

<p>Here is a fairly general solution to the problem using some <code>itertools</code> functions. That solution will collapse a repetition of min and max values into a single value wherever that repetition may appear:</p>

<pre><code>from itertools import groupby,starmap,chain
#first we count the sequence length for each group of number in the list
c=[[number,len(list(seq))] for number,seq in groupby(numbers)]
print(c)

#we collapse the repetitions only for min and max
for group in c:
    if group[0] == min(numbers) or group[0]== max(numbers):
        group[1]=1       
print(c)

#we put back the list together
numbers=list(chain(*starmap(lambda x,n : [x]*n, c)))
print(numbers)
</code></pre>

<p>Example output:</p>

<pre><code>&gt;&gt;&gt;numbers=[1,1,1,4,4,5,6,8,8,8]
[1, 4, 4, 5, 6, 8]
&gt;&gt;&gt;numbers=[4,4,1,1,1,5,6,8,8,8]
[4, 4, 1, 5, 6, 8]
#edge case if there are multiple groups of min :
&gt;&gt;&gt;numbers=[4,4,1,1,1,5,1,1,1,6,8,8,8]
[4, 4, 1, 5, 1, 6, 8]
</code></pre>

python