python3:set 和 itertools.groupby 产生不同的结果? [复制]

Posted

技术标签:

【中文标题】python3:set 和 itertools.groupby 产生不同的结果? [复制]【英文标题】:python3: set and itertools.groupby yielding different outcomes? [duplicate] 【发布时间】:2019-11-21 11:14:49 【问题描述】:

我想使用IPWhois 解析一些 Apache 访问日志。

我想根据asn_description 字段对IPWhois 结果进行分组。

以下sn-p中的setitertools.groupby()是不是会产生不同的结果?

descs = set()

with open(RESULTSFILE, 'a+') as r:
    for description, items in groupby(results, key=lambda x: x['asn_description']):
        print('ASN Description: ' + description)
        descs.add(description)

print(descs)

例如

ASN Description: GOOGLE - Google LLC, US
ASN Description: AVAST-AS-DC, CZ
ASN Description: FACEBOOK - Facebook, Inc., US
ASN Description: AVAST-AS-DC, CZ
ASN Description: AMAZON-AES - Amazon.com, Inc., US
ASN Description: FACEBOOK - Facebook, Inc., US
ASN Description: AMAZON-02 - Amazon.com, Inc., US
ASN Description: AMAZON-02 - Amazon.com, Inc., US
ASN Description: GOOGLE - Google LLC, US
ASN Description: GOOGLE-2 - Google LLC, US
ASN Description: AMAZON-02 - Amazon.com, Inc., US
'FACEBOOK - Facebook, Inc., US', 'AVAST-AS-DC, CZ', 'AMAZON-AES - Amazon.com, Inc., US', 'GOOGLE-2 - Google LLC, US', 'GOOGLE - Google LLC, US', 'AMAZON-02 - Amazon.com, Inc., US',

【问题讨论】:

Set 会返回唯一的值,groupby 只返回连续的唯一值 【参考方案1】:

将您的代码更改为以下内容并尝试。如果您不需要items,则可以使用_ 将其从for 循环中删除。

import itertools
descs = dict()

with open(RESULTSFILE, 'a+') as r:
    for i, (description, items) in enumerate(itertools.groupby(results, key=lambda x: x['asn_description'])):
        print('ASN Description: ' + description)
        descs.update(i: description)

print(descs)

【讨论】:

以上是关于python3:set 和 itertools.groupby 产生不同的结果? [复制]的主要内容,如果未能解决你的问题,请参考以下文章

python3:set 和 itertools.groupby 产生不同的结果? [复制]

python3-disc和set

python3.6 基础之路 —— Sets集合

Python3x: set中union和update的区别

python3 Set集合与数据转换函数

python3学习笔记_dict-set