我有一个列表,我可以按任何给定的值排序。稍后我可以使用我用来对其进行排序的相同密钥对此列表进行分组:
oFile = csv.reader(open(FileCsv,"r"), delimiter=",", quotechar="|")
routers = list(oFile)
routers_sorted=sorted(routers, key=itemgetter(0))
groups = groupby(routers_sorted, key=itemgetter(0))
到目前为止一切顺利。事情是我不知道如何访问分组列表的数据:
for i,rou in enumerate(groups): print i,rou
0 ('10.2.53.6', <itertools._grouper object at 0x7f2585681850>)
1 ('10.2.54.10', <itertools._grouper object at 0x7f2585681890>)
2 ('10.2.54.33', <itertools._grouper object at 0x7f2585681850>)
3 ('10.2.54.35', <itertools._grouper object at 0x7f2585681890>)
4 ('10.2.54.8', <itertools._grouper object at 0x7f2585681850>)
5 ('10.2.56.58', <itertools._grouper object at 0x7f2585681890>)
实际上每个组中都有很多字段。例如:
for i,rou in enumerate(routers_sorted): print i,rou
0 ['10.2.53.6', '4001']
1 ['10.2.54.10', '4001']
2 ['10.2.54.10', '5000']
3 ['10.2.54.33', '4001']
4 ['10.2.54.33', '5000']
5 ['10.2.54.35', '4001']
6 ['10.2.54.35', '5000']
7 ['10.2.54.8', '4001']
8 ['10.2.54.8', '5000']
9 ['10.2.56.58', '4001']
10 ['10.2.56.58', '5000']
因此对于关键字&lt; 10.2.54.33&#39;你有两个记录:&#39; 5000&#39;和&#39; 4001&#39;。列表分组后如何访问这些文件?
答案 0 :(得分:1)
不完全确定你在问什么。那些grouper
对象是特殊的迭代器,您可以将它们转换为列表,例如在循环或列表理解中。
>>> for i, group in groups:
>>> print i, list(group)
...
10.2.54.10 [['10.2.54.10', '4001'], ['10.2.54.10', '5000']]
...
但是创建字典可能更有用,将您分组的键映射到其他值:
>>> group_dict = {i: map(itemgetter(1), group) for i, group in groups}
{'10.2.54.8': ['4001', '5000'],
'10.2.53.6': ['4001'],
'10.2.56.58': ['4001', '5000'],
'10.2.54.10': ['4001', '5000'],
'10.2.54.33': ['4001', '5000'],
'10.2.54.35': ['4001', '5000']}
当然,您实际上可以跳过groupby
并只使用dict
和循环:
group_dict = {}
for key, val in routers_sorted:
group_dict.setdefault(key, []).append(val)
或者,如果订单很重要,请对OrderedDict