如何改进嵌套理解的性能?

huangapple go评论109阅读模式
英文:

How can I improve performance of nested comprehension?

问题

我尝试使用Python 3.x的推导式来创建一个嵌套字典结构。我的推导式语法有效,但对于大型数据集来说速度非常慢。我已经使用循环创建了我想要的数据结构,它运行得更快,但我想知道是否有方法可以改进这个推导式,使其更有效率,可能能够与我的循环代码一样快甚至更快地运行。

我的输入数据是一个包含字典的列表,每个字典都描述了业余无线电联系(日志条目)的具体信息。以下是我的数据的随机子集(限制为20个条目,并删除了字典中的非关键信息,以使其更清晰):

我想创建一个字典,其中每个键都是一个波段(10M、20M等),值将是一个字典,列出该波段上联系的国家作为键,每个国家上的联系计数作为值。以下是我的输出示例:

这是我想出的用于创建输出的推导式。它有效,对于这里显示的有限数据集,运行速度很快,但对于包含几千个条目的输入列表,运行时间很长。

worked_dxcc_by_band = {
    z["BAND"]: {
        x["COUNTRY"]: len([y["COUNTRY"]
                           for y in log_entries
                           if y["COUNTRY"] == x["COUNTRY"] and y["BAND"] == z["BAND"]])
        for x in log_entries
        if x["BAND"] == z["BAND"]
    }
    for z in log_entries
}

由于这是一个三重嵌套的推导式,所有三个循环都遍历整个log_entries列表,我认为这就是为什么它变得非常慢的原因。

是否有更有效的方法可以使用推导式来完成这个任务?我可以使用循环处理数据,但我试图提高使用推导式的技能,所以我认为这将是一个很好的练习!

这是我在不使用推导式的情况下所做的:我有一个名为analyze_log_entry的函数,每次从文件中加载一个日志条目时都会调用它。

from collections import Counter

worked_dxcc_by_band = {}

def analyze_log_entry(entry):
    if "BAND" in entry:
        if "COUNTRY" in entry:
            if entry["BAND"] in worked_dxcc_by_band:
                worked_dxcc_by_band[entry["BAND"]][entry["COUNTRY"]] += 1
            else:
                worked_dxcc_by_band[entry["BAND"]] = Counter()
                worked_dxcc_by_band[entry["BAND"]][entry["COUNTRY"]] = 1

这本身可能不是非常高效,但我的完整代码在analyze_log_entry函数中有许多类似的块,用于构建多个字典。因为我只遍历我的数据一次,并在适当的地方构建字典,所以它可能比使用推导式更高效,后者本质上是多个循环。正如我所说,这更多地是一个练习,以学习如何使用不同的方法完成相同的任务。

英文:

I am trying to use python 3.x comprehension to create a nested dictionary structure. My comprehension syntax works, but it is very slow, especially with a large data set. I have also created my desired data structure using loops and it runs much faster, but I would like to know if there is a way to improve this comprehension to make it more efficient and potentially run as fast as, or faster than my loop code.

My input data is a list of dictionaries, each dictionary outlining the specifics of an amateur radio contact (log entry). Here is a random subset of my data (limited to 20 entries, and non-essential keys in the dictionary removed to make this more clear)

[{'BAND': '20M',
  'CALL': 'AA9GL',
  'COUNTRY': 'UNITED STATES OF AMERICA',
  'QSO_DATE': '20170528',
  'TIME_ON': '132100'},
 {'BAND': '20M',
  'CALL': 'KE4BFI',
  'COUNTRY': 'UNITED STATES OF AMERICA',
  'QSO_DATE': '20150704',
  'TIME_ON': '034600'},
 {'BAND': '20M',
  'CALL': 'W8OTR',
  'COUNTRY': 'UNITED STATES OF AMERICA',
  'QSO_DATE': '20190119',
  'TIME_ON': '194645'},
 {'BAND': '10M',
  'CALL': 'FY5FY',
  'COUNTRY': 'FRENCH GUIANA',
  'QSO_DATE': '20150328',
  'TIME_ON': '161953'},
 {'BAND': '17M',
  'CALL': 'KD5FOY',
  'COUNTRY': 'UNITED STATES OF AMERICA',
  'QSO_DATE': '20190121',
  'TIME_ON': '145630'},
 {'BAND': '10M',
  'CALL': 'K5GQ',
  'COUNTRY': 'UNITED STATES OF AMERICA',
  'QSO_DATE': '20150110',
  'TIME_ON': '195326'},
 {'BAND': '10M',
  'CALL': 'CR5L',
  'COUNTRY': 'PORTUGAL',
  'QSO_DATE': '20151025',
  'TIME_ON': '182351'},
 {'BAND': '20M',
  'CALL': 'AD4TR',
  'COUNTRY': 'UNITED STATES OF AMERICA',
  'QSO_DATE': '20170325',
  'TIME_ON': '144606'},
 {'BAND': '40M',
  'CALL': 'EA8FJ',
  'COUNTRY': 'CANARY ISLANDS',
  'QSO_DATE': '20170618',
  'TIME_ON': '020300'},
 {'BAND': '10M',
  'CALL': 'PY2DPM',
  'COUNTRY': 'BRAZIL',
  'QSO_DATE': '20150104',
  'TIME_ON': '205900'},
 {'BAND': '17M',
  'CALL': 'MM0HVU',
  'COUNTRY': 'SCOTLAND',
  'QSO_DATE': '20170416',
  'TIME_ON': '130200'},
 {'BAND': '10M',
  'CALL': 'LW3DG',
  'COUNTRY': 'ARGENTINA',
  'QSO_DATE': '20161029',
  'TIME_ON': '210629'},
 {'BAND': '10M',
  'CALL': 'LW3DG',
  'COUNTRY': 'ARGENTINA',
  'QSO_DATE': '20151025',
  'TIME_ON': '210714'},
 {'BAND': '20M',
  'CALL': 'EI7HDB',
  'COUNTRY': 'IRELAND',
  'QSO_DATE': '20170423',
  'TIME_ON': '184000'},
 {'BAND': '20M',
  'CALL': 'KM0NAS',
  'COUNTRY': 'UNITED STATES OF AMERICA',
  'QSO_DATE': '20180102',
  'TIME_ON': '142151'},
 {'BAND': '10M',
  'CALL': 'PY2TKB',
  'COUNTRY': 'BRAZIL',
  'QSO_DATE': '20150328',
  'TIME_ON': '223535'},
 {'BAND': '40M',
  'CALL': 'EB1DJ',
  'COUNTRY': 'SPAIN',
  'QSO_DATE': '20170326',
  'TIME_ON': '232430'},
 {'BAND': '40M',
  'CALL': 'LU6PCK',
  'COUNTRY': 'ARGENTINA',
  'QSO_DATE': '20150615',
  'TIME_ON': '000200'},
 {'BAND': '17M',
  'CALL': 'G3RKF',
  'COUNTRY': 'ENGLAND',
  'QSO_DATE': '20190121',
  'TIME_ON': '144315'},
 {'BAND': '20M',
  'CALL': 'UA1ZKI',
  'COUNTRY': 'EUROPEAN RUSSIA',
  'QSO_DATE': '20170508',
  'TIME_ON': '141400'}]

I want to create a dictionary where each key is a band (10M, 20M, etc) and the value will be a dictionary listing the counties contacted on that band as keys and a count of contacts for each country on that band as the values. Here is what my output looks like:

{'10M': {'ARGENTINA': 2,
         'BRAZIL': 2,
         'FRENCH GUIANA': 1,
         'PORTUGAL': 1,
         'UNITED STATES OF AMERICA': 1},
 '17M': {'ENGLAND': 1, 'SCOTLAND': 1, 'UNITED STATES OF AMERICA': 1},
 '20M': {'EUROPEAN RUSSIA': 1, 'IRELAND': 1, 'UNITED STATES OF AMERICA': 5},
 '40M': {'ARGENTINA': 1, 'CANARY ISLANDS': 1, 'SPAIN': 1}}

This is the comprehension that I came up with to create the output. It works, and with the limited data set shown here, it runs quickly, but with an input list of a couple thousand entries, it takes quite a long time to run.

worked_dxcc_by_band = {
    z["BAND"]: {
        x["COUNTRY"]: len([y["COUNTRY"]
                           for y in log_entries
                           if y["COUNTRY"] == x["COUNTRY"] and y["BAND"] == z["BAND"]])
        for x in log_entries
        if x["BAND"] == z["BAND"]
    }
    for z in log_entries
}

Because this is a triple-nested comprehension, and all 3 loops run through the entire log_entries list, I am assuming that is why it gets very slow.

Is there a more efficient way to accomplish this with comprehension? I am fine using my loop to process the data but I am trying to enhance my skills regarding comprehensions so I thought this would be a good exercise!

This is what I am doing without using comprehension: I have a function analyize_log_entry which I call as I load each log entry in from a file.

from collections import Counter

worked_dxcc_by_band = {}


def analyze_log_entry(entry):
    if "BAND" in entry:
        if "COUNTRY" in entry:
            if entry["BAND"] in worked_dxcc_by_band:
                worked_dxcc_by_band[entry["BAND"]][entry["COUNTRY"]] += 1
            else:
                worked_dxcc_by_band[entry["BAND"]] = Counter()
                worked_dxcc_by_band[entry["BAND"]][entry["COUNTRY"]] = 1

This in itself may not be that efficient but my full code has many similar blocks within the analyze_log_entry function that build multiple dictionaries. Because I am only going through all of my data once, and building the dictionaries where appropriate, it is probably much more efficient than using comprehension, which is essentially multiple loops. As I said, this is more of an exercise to learn how to accomplish the same task with different methods.

答案1

得分: 3

以下是代码的中文翻译部分:

# 使用字典推导式版本:
out = {band: dict(Counter(v['COUNTRY'] for v in g)) for band, g in groupby(sorted(data, key=lambda k: k['BAND']), lambda k: k['BAND'])}

# 可以结合 itertools.groupby 和 collections.Counter:
from itertools import groupby
from collections import Counter

s = sorted(data, key=lambda k: k['BAND'])

out = {}
for band, g in groupby(s, lambda k: k['BAND']):
    c = Counter(v['COUNTRY'] for v in g)
    out[band] = dict(c)

# 不使用模块的版本:
out = {}
for i in data:
    out.setdefault(i['BAND'], {}).setdefault(i['COUNTRY'], 0)
    out[i['BAND']][i['COUNTRY']] += 1

# 基准测试:
from timeit import timeit

from itertools import groupby
from collections import Counter

def sol_orig():
    worked_dxcc_by_band = {z["BAND"]: {x["COUNTRY"] : len([y["COUNTRY"] for y in data if y["COUNTRY"] == x["COUNTRY"] and y["BAND"] == z["BAND"]]) for x in data if x["BAND"] == z["BAND"]} for z in data}
    return worked_dxcc_by_band

def solution():
    out = {band: dict(Counter(v['COUNTRY'] for v in g)) for band, g in groupby(sorted(data, key=lambda k: k['BAND']), lambda k: k['BAND'])}
    return out

def solution_2():
    out = {}
    for i in data:
        out.setdefault(i['BAND'], {}).setdefault(i['COUNTRY'], 0)
        out[i['BAND']][i['COUNTRY']] += 1
    return out

t1 = timeit(lambda: solution(), number=10000)
t2 = timeit(lambda: solution_2(), number=10000)
t3 = timeit(lambda: sol_orig(), number=10000)

print(t1)
print(t2)
print(t3)

请注意,这只是代码的翻译部分,不包括任何问题的回答。

英文:

EDIT: Dictionary comprehension version:

out = {band: dict(Counter(v['COUNTRY'] for v in g)) for band, g in groupby(sorted(data, key=lambda k: k['BAND']), lambda k: k['BAND'])}

You can combine itertools.groupby and collections.Counter:

from itertools import groupby
from collections import Counter

s = sorted(data, key=lambda k: k['BAND'])

out = {}
for band, g in groupby(s, lambda k: k['BAND']):
    c = Counter(v['COUNTRY'] for v in g)
    out[band] = dict(c)

from pprint import pprint
pprint(out)

Prints:

{'10M': {'ARGENTINA': 2,
         'BRAZIL': 2,
         'FRENCH GUIANA': 1,
         'PORTUGAL': 1,
         'UNITED STATES OF AMERICA': 1},
 '17M': {'ENGLAND': 1, 'SCOTLAND': 1, 'UNITED STATES OF AMERICA': 1},
 '20M': {'EUROPEAN RUSSIA': 1, 'IRELAND': 1, 'UNITED STATES OF AMERICA': 5},
 '40M': {'ARGENTINA': 1, 'CANARY ISLANDS': 1, 'SPAIN': 1}}

EDIT: Without modules:

out = {}
for i in data:
    out.setdefault(i['BAND'], {}).setdefault(i['COUNTRY'], 0)
    out[i['BAND']][i['COUNTRY']] += 1

from pprint import pprint
pprint(out)

Benchmark:

from timeit import timeit

from itertools import groupby
from collections import Counter

def sol_orig():
    worked_dxcc_by_band = {z["BAND"]: {x["COUNTRY"] : len([y["COUNTRY"] for y in data if y["COUNTRY"] == x["COUNTRY"] and y["BAND"] == z["BAND"]]) for x in data if x["BAND"] == z["BAND"]} for z in data}
    return worked_dxcc_by_band

def solution():
    out = {band: dict(Counter(v['COUNTRY'] for v in g)) for band, g in groupby(sorted(data, key=lambda k: k['BAND']), lambda k: k['BAND'])}
    return out

def solution_2():
    out = {}
    for i in data:
        out.setdefault(i['BAND'], {}).setdefault(i['COUNTRY'], 0)
        out[i['BAND']][i['COUNTRY']] += 1
    return out

t1 = timeit(lambda: solution(), number=10000)
t2 = timeit(lambda: solution_2(), number=10000)
t3 = timeit(lambda: sol_orig(), number=10000)

print(t1)
print(t2)
print(t3)

Prints:

0.18113317096140236
0.08159565401729196
3.5367472909856588

huangapple
  • 本文由 发表于 2020年1月7日 00:41:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/59615835.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定