我有一份清单清单。列表由来自某些区域的人组成,如果列表中有太多人来自某个区域,我想从列表集中删除列表。列表长度为9
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)>3:
den.append(1)
if (ab+ac+ad+ae+af+ag+ah+ai+aj)<4:
den.append(0)
denk.append(sum(den))
finalList=[]
for i, j in enumerate(denk):
if j == 0:
finalList.append(list[i])
我试图计算来自城市的人数,如果人数大于3我尝试追加1,如果不是0.我只这样做,所以我可以总结列表的次数超过配额。
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"]]
在测试这个特定列表时,测试看看有多少人来自加利福尼亚会使den = 1,因为来自加利福尼亚的人数超过3人。下一个城市,内华达州,也将使den = 1,依此类推...... DEN = [1,1,0,0,0,0] DENK = [2] 所以这个列表被抛出
[["Abital","California"],
["Abitub","California"],
["Absalom","Nevada"],
["Accad","Nevada"],
["Agar","Utah"],
["Agee","Utah"],
["Aijeleth-Shahar","New Mexico"],
["Ain","New Mexico"],
["Amram","Washington"]]
在此处执行相同操作会为Cities中的每个城市生成den = 0,den = [0,0,0,0,0,0],denk = [0],因此列表将被接受。
finalList不应该包含来自一个地方的人太多的列表。
答案 0 :(得分:1)
假设您从以下内容开始:
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"]]]
要在每个二级列表中查找分布,您可以使用列表理解和collections.Counter
:
import collections
>>> [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})]
要查找每个二级列表中最常见的计数,您可以使用
>>> [collections.Counter(e[1] for e in l).most_common(1)[0][1] for l in list]
[4, 2]
因此,要仅保留最常见计数最多的二级列表,例如3,您可以使用
>>> [l for l in list if collections.Counter(e[1] for e in l).most_common(1)[0][1] <= 3]
[[['Abital', 'California'],
['Abitub', 'California'],
['Absalom', 'Nevada'],
['Accad', 'Nevada'],
['Agar', 'Utah'],
['Agee', 'Utah'],
['Aijeleth-Shahar', 'New Mexico'],
['Ain', 'New Mexico'],
['Amram', 'Washington']]]