Python 字典:打印字典中从最大到最小项目的整个列表
大家好,我有一个关于根据字典中的项目数将最大值打印为最小值的问题 我需要一个完整的键列表打印,并在键旁边显示次数 基于这两个列表:Python 字典:打印字典中从最大到最小项目的整个列表,python,dictionary,max,key-value,Python,Dictionary,Max,Key Value,大家好,我有一个关于根据字典中的项目数将最大值打印为最小值的问题 我需要一个完整的键列表打印,并在键旁边显示次数 基于这两个列表: Teams = ['Boston Americans','World Series Not Played in 1904','New York Giants', 'Chicago White Sox','Chicago Cubs','Chicago Cubs','Pittsburgh Pirates', 'Philadelphia
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
我正试图从制造
团队=关键,年份=价值芝加哥小熊队:[19071908] 团队=键和显示次数=值
芝加哥小熊队:[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
虽然我在第二个函数中更接近,但我只需要数字。有没有办法解决这个问题?感谢您的任何想法或指导。您的意思是这样的吗
from collections import defaultdict
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=defaultdict(list)
for x,y in zip(Teams,Year):
d[x].append(y)
for k,v in sorted(d.items(),key=lambda y:len(y[-1]),reverse=True):
print "{0} {1}".format(k,",".join(map(str,v)))
输出:
Chicago Cubs 1907,1908
Chicago White Sox 1906
New York Giants 1905
World Series Not Played in 1904 1904
Philadelphia Athletics 1910
Pittsburgh Pirates 1909
Boston Americans 1903
不太清楚你想在这里做什么。也许读一点默写本?重复钥匙->允许吗?以下是我如何理解您的问题的解决方案:
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]
l = zip(Teams, Year)
d = {}
for e in l:
if e[0] not in d:
d[e[0]] = [e[1]]
else:
d[e[0]].append(e[1])
#Max Item
s = sorted(d.items(), key=lambda x: len(x[1]))[-1]
print("%s - %s" % (s[0], len(s[1])))
# Entire list
for k, v in sorted(d.items(), key=lambda x: -len(x[1])):
print("%s - %s" % (k, len(v)))
这是一个解决方案,如果你真的需要口述后。注意,排序根据上面生成的列表长度将chicago放在最后一个位置。然后通过[-1]得到最后一项。什么是
D={[波士顿美国人:1903],…
是什么意思?它不是有效的Python。D
是写入team:year
的字典。为什么是方括号?为什么是没有引号的字符串?请发布有效的Python代码。很抱歉,我的错误已修复。感谢您的帮助。非常接近,但来自键:value
或team:year
I需要项目列表中赢得的年数。芝加哥小熊队:19071908
=Chicogo小熊队,2
@ThomasJones这是你想要的吗?我将在我个人项目的下一步中使用它,谢谢!!!这是我长期工作的重要参考资料。是的,这很好!我更新了帖子。N注意:现在len(x[1])是-len(x[1])。这将按整数的相反顺序排序。->[3,2,1]->[1,2,3][-1,-2,-3]>[-3,-2,-1]。太好了。你应该接受答案。注意Ashwini Chaudhary答案稍微短一点,我们基本上也是这样做的。另外,如果你删除len()从要打印的密钥中,您将获得年份列表。