开发者

Categorize a list of lists by 1 element in python

An example list of lists:

[
["url","name","date","category"]
["hello","world","2010","one category"]
["foo","bar","2010","another category"]
["asdfasdf","adfasdf","2010","one category"]
["qwer","req","2010","another category"]
]

What I wish do to 开发者_开发问答is create a dictionary -> category : [ list of entries ].

The resultant dictionary would be:

{"category" : [["url","name","date","category"]],
"one category" : [["hello","world","2010","one category"],["asdfasdf","adfasdf","2010","one category"]],
"another category" : [["foo","bar","2010","another category"], ["qwer","req","2010","another category"]]}


dict((category, list(l)) for category, l 
     in itertools.groupby(l, operator.itemgetter(3))

The main thing here is the usage of itertools.groupby. It simply returns iterables instead of lists, which is why there's a call for list(l), which means that if you're ok with that, you can simply write dict(itertools.groupby(l, operator.itemgetter(3)))


newdict = collections.defaultdict(list)
for entry in biglist:
  newdict[entry[3]].append(entry)


A variation on ghostdog74's answer, which fully uses the semantics of setdefaults:

result={}
for li in list_of_lists:
    result.setdefault(li[-1], []).append(li)


list_of_lists=[
["url","name","date","category"],
["hello","world","2010","one category"],
["foo","bar","2010","another category"],
["asdfasdf","adfasdf","2010","one category"],
["qwer","req","2010","another category"]
]
d={}
for li in list_of_lists:
    d.setdefault(li[-1], [])
    d[ li[-1] ].append(li)
for i,j in d.iteritems():
    print i,j



d = {}
for e in l:
    if e[3] in d:
        d[e[3]].append(e)
    else:
        d[e[3]] = [e]


>>> l = [
... ["url","name","date","category"],
... ["hello","world","2010","one category"],
... ["foo","bar","2010","another category"],
... ["asdfasdf","adfasdf","2010","one category"],
... ["qwer","req","2010","another category"],
... ]
#Intermediate list to generate a more dictionary oriented data
>>> dl = [ (li[3],li[:3]) for li in l ]
>>> dl
[('category', ['url', 'name', 'date']), 
 ('one category', ['hello', 'world', '2010']), 
 ('another category', ['foo', 'bar', '2010']), 
 ('one category', ['asdfasdf', 'adfasdf', '2010']), 
 ('another category', ['qwer', 'req', '2010'])]
#Final dictionary
>>> d = {}
>>> for cat, data in dl:
...   if cat in d:
...     d[cat] = d[cat] + [ data ]
...   else:
...     d[cat] = [ data ]
...
>>> d
{'category': [['url', 'name', 'date']], 
 'one category': [['hello', 'world', '2010'], ['asdfasdf', 'adfasdf', '2010']], 
 'another category': [['foo', 'bar', '2010'], ['qwer', 'req', '2010']]}

The final data it's a little different as I haven't included on the data the category (seems quite pointless to me), but you can add it easily, if needed...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜