简体   繁体   English

在python中按1个元素对列表列表进行分类

[英]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 . 这里最主要的是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))) 它只是返回iterables而不是list,这就是为什么要调用list(l) ,这意味着如果您对此表示满意,则可以简单地编写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: 对ghostdog74的回答的一种变体,它完全使用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... 最终数据有所不同,因为我没有将数据包括在类别中(对我来说似乎毫无意义),但是如果需要,您可以轻松添加...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM