我有一个这样的清单
[u'201003', u'200403', u'200803', u'200503', u'201303',
u'200903', u'200603', u'201203', u'200303', u'200703', u'201103']
让我们将此列表称为“years_list”
当我按年分组时,
group_by_yrs_list = groupby(years_list, key = lambda year_month: year_month[:-2])
for k,v in group_by_yrs_list:
print k, list(v)
我得到了想要的输出:
2010 [u'201003']
2004 [u'200403']
2008 [u'200803']
2005 [u'200503']
2013 [u'201303']
2009 [u'200903']
2006 [u'200603']
2012 [u'201203']
2003 [u'200303']
2007 [u'200703']
2011 [u'201103']
然后,我像这样稍微改变了我的实现,
group_by_yrs_list = dict(groupby(years_list, key = lambda year_month: year_month[:-2]))
for k,v in group_by_yrs_list.items():
print k, list(v)
我刚刚添加了一个dict,但是输出不同,
2003 []
2006 []
2007 []
2004 []
2005 []
2008 []
2009 []
2011 [u'201103']
2010 []
2013 []
2012 []
我找不到原因。请帮我找出字典实际上在做什么。
(Python 2.7)