みなさん、こんにちは。辞書の項目数に基づいて最大値を最小値に出力することについて質問があります。
キーの横に表示されている回数が印刷されたキーリスト全体が必要です。
これらの 2 つのリストに基づいて:
Teams = ['Boston Americans','World Series Not Played in 1904','New York Giants',
'Chicago White Sox','Chicago Cubs','Chicago Cubs','Pittsburgh Pirates',
'Philadelphia Athletics']
Year = [1903,1904,1905,1906,1907,1908,1909,1910]
各チームが持っているアイテムの数に基づいて、次を印刷するにはどうすればよいですか。
D = {Teams,Year}
これをtxtファイルから読んでいることを忘れていました。
アップデート
def Team_data(team,year):
D = defaultdict(list)
for team,year in zip(team,year):
D[team].append(year)
pprint(D)
return D
その後、次のように返されます。
D = {'Boston Americans':1903,'World Series Not Played in 1904':1904,
'New York Giants':1905,'Chicago White Sox': 1906,'Chicago Cubs': 1907,
'Chicago Cubs':1908,'Pittsburgh Pirates':1909,'Philadelphia Athletics':1910}
Team_max = []
印刷すると、リストに次のように表示されます
Chicago Cubs, 2
Boston Americans, 1
World Series Not Played in 1904, 1
New York Giants, 1
Chicago White Sox, 1
Pittsburgh Pirates, 1
Philadelphia Athletics, 1
私は作ることから行こうとしています
チーム = キーと年 = 値
Chicago Cubs:[1907,1908]
チーム = キーと表示回数 = 値
Chicago Cubs:[2]
私が読んだことに基づいて、以下を使用してみました:
def Team_data_max(D):
key = D.keys()
value = D.values()
team_max = []
team_max = sorted(D, key=lambda key: len(D[key]))
print(team_max)
#Print 1 Key based and 1 max number of items in dictionary
#Chicago Cubs, 2
またはこれ
team_max = []
team_max = max(((k, len(v)) for k, v in D.items()), key=lambda x: x[1])
print(Series_max)
#Print the entire key list least to greatest without a number which is what I need
#based on the items list
そして、これ
s = []
s = [k for k in D.keys() if len(D.get(k))==max([len(n) for n in D.values()])]
print(s)
#print max key without number
#Chicago Cubs
私は2番目の関数でずっと近づいていますが、番号だけが必要です。これに取り組む方法についてのアイデアはありますか?任意の考えやガイダンスをいただければ幸いです。