Testing List of Lists to remove unwanted lists based on a constraint
<p>I have a list of lists. The lists are made up of people from certain areas, if the lists have too many people from a certain area I would like to remove the list from the set of lists. The lists are lengths of 9</p>

<pre><code>list=[[["Aarat","California"],
["Aaron","California"],
["Abba","California"],
["Abaddon","California"],
["Abner","Nevada"],
["Abram","Nevada"],
["Abraham","Nevada"],
["Absalom","Nevada"],
["Adullam","Utah"]],
......,
[["Abital","California"],
["Abitub","California"],
["Absalom","Nevada"],
["Accad","Nevada"],
["Agar","Utah"],
["Agee","Utah"],
["Aijeleth-Shahar","New Mexico"],
["Ain","New Mexico"],
["Amram","Washington"]]]
Cities=["California","Nevada","Utah","New Mexico","Idaho","Washington"]
denk=[] 
for city in Cities:
    den=[]
    for i in list:
        a=i[0]
        b=i[1]
        c=i[2]
        d=i[3]
        e=i[4]
        f=i[5]
        g=i[6]
        h=i[7]
        k=i[8]
        if a==city:
            ab=1
        if b==city:
            ac=1
        if c==city:
            ad=1
        if d==city:
            ae=1
        if e==city:
            af=1
        if f==city:
            ag=1
        if g==city:
            ah=1
        if h==city:
            ai=1
        if k==city:
            aj=1
        if (ab+ac+ad+ae+af+ag+ah+ai+aj)&gt;3:
            den.append(1)
        if (ab+ac+ad+ae+af+ag+ah+ai+aj)&lt;4:
            den.append(0)
    denk.append(sum(den))

finalList=[]
for i, j in enumerate(denk):
    if j == 0:
        finalList.append(list[i])
</code></pre>

<p>I attempt to count the amount of people from the city, if the amount of people is greater than 3 I try to append a 1, if not 0. I only do this so i can sum up the amount of times the list goes over the quota. </p>

<pre><code>Cities=["California","Nevada","Utah","New Mexico","Idaho","Washington"]

[["Aarat","California"],
["Aaron","California"],
["Abba","California"],
["Abaddon","California"],
["Abner","Nevada"],
["Abram","Nevada"],
["Abraham","Nevada"],
["Absalom","Nevada"],
["Adullam","Utah"]]
</code></pre>

<p>In testing this particular list the testing to see how many people are from California would make den=1 because there are more than 3 people from California. The next city, Nevada, would also make den=1, and so on....
den=[1,1,0,0,0,0]
denk=[2]
So this list gets thrown out</p>

<pre><code>[["Abital","California"],
["Abitub","California"],
["Absalom","Nevada"],
["Accad","Nevada"],
["Agar","Utah"],
["Agee","Utah"],
["Aijeleth-Shahar","New Mexico"],
["Ain","New Mexico"],
["Amram","Washington"]]
</code></pre>

<p>Doing the same here yields den=0 for each city in Cities, den=[0,0,0,0,0,0], denk=[0] so the list will be accepted.</p>

<p>The finalList should not have any lists that have too many people from one place.</p>

<p>Say you start with something like:</p>

<pre><code>list=[[["Aarat","California"],
    ["Aaron","California"],
    ["Abba","California"],
    ["Abaddon","California"],
    ["Abner","Nevada"],
    ["Abram","Nevada"],
    ["Abraham","Nevada"],
    ["Absalom","Nevada"],
    ["Adullam","Utah"]],[["Abital","California"],
    ["Abitub","California"],
    ["Absalom","Nevada"],
    ["Accad","Nevada"],
    ["Agar","Utah"],
    ["Agee","Utah"],
    ["Aijeleth-Shahar","New Mexico"],
    ["Ain","New Mexico"],
    ["Amram","Washington"]]]
</code></pre>

<p>To find the distribution within each second-level list, you could use list comprehension and <a href="https://docs.python.org/2/library/collections.html#collections.Counter" rel="nofollow"><code>collections.Counter</code></a>:</p>

<pre><code>import collections

&gt;&gt;&gt; [collections.Counter(e[1] for e in l) for l in list]
[Counter({'California': 4, 'Nevada': 4, 'Utah': 1}),
 Counter({'California': 2,
          'Nevada': 2,
          'New Mexico': 2,
          'Utah': 2,
          'Washington': 1})]
</code></pre>

<p>To find the most common count within each second-level list, you could use</p>

<pre><code>&gt;&gt;&gt; [collections.Counter(e[1] for e in l).most_common(1)[0][1] for l in list]
[4, 2]
</code></pre>

<p>So, to retain only second-level lists where the most common count is at most, say, 3, you could just use</p>

<pre><code>&gt;&gt;&gt; [l for l in list if collections.Counter(e[1] for e in l).most_common(1)[0][1] &lt;= 3]
[[['Abital', 'California'],
  ['Abitub', 'California'],
  ['Absalom', 'Nevada'],
  ['Accad', 'Nevada'],
  ['Agar', 'Utah'],
  ['Agee', 'Utah'],
  ['Aijeleth-Shahar', 'New Mexico'],
  ['Ain', 'New Mexico'],
  ['Amram', 'Washington']]]
</code></pre>

python algorithm list