快速/Pythonic方法来计算重复列表值之间的间隔

2024-04-24 14:01:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我想做一个列表中重复值之间所有间隔的直方图。我写了一些代码,但它使用了if语句的for循环。我经常发现,如果一个人能够使用巧妙的切片和/或预定义的python(numpy)方法来编写一个版本,那么就可以得到比使用for循环快得多的python代码,但是在这种情况下,我想不出任何方法来做到这一点。有人能提出一个更快或更具Python的方法吗?你知道吗

# make a 'histogram'/count of all the intervals between repeated values
def hist_intervals(a):
    values = sorted(set(a))  # get list of which values are in a

    # setup the dict to hold the histogram
    hist, last_index = {}, {}
    for i in values:
        hist[i] = {}
        last_index[i] = -1   # some default value

    # now go through the array and find intervals
    for i in range(len(a)):
        val = a[i]
        if last_index[val] != -1:   # do nothing if it's the first time
            interval = i - last_index[val]
            if interval in hist[val]:
                hist[val][interval] += 1
            else:
                hist[val][interval] = 1
        last_index[val] = i
    return hist

# example list/array
a = [1,2,3,1,5,3,2,4,2,1,5,3,3,4]

histdict = hist_intervals(a)

print("histdict = ",histdict)

# correct answer for this example
answer = {  1: {3:1, 6:1},
            2: {2:1, 5:1},
            3: {1:1, 3:1, 6:1},
            4: {6:1},
            5: {6:1}
            }
print("answer =   ",answer)

样本输出:

histdict =  {1: {3: 1, 6: 1}, 2: {5: 1, 2: 1}, 3: {3: 1, 6: 1, 1: 1}, 4: {6: 1}, 5: {6: 1}}
answer =    {1: {3: 1, 6: 1}, 2: {2: 1, 5: 1}, 3: {1: 1, 3: 1, 6: 1}, 4: {6: 1}, 5: {6: 1}}

^注意:我不关心dict中的顺序,因此此解决方案是可以接受的,但我希望能够在真正大型数组/列表上运行,我怀疑我当前的方法会很慢。你知道吗


Tags: the方法answerin列表forindexif
2条回答

在数据结构方面有一个明显的变化。与其使用hist的字典,不如使用defaultdictCounter这让代码变得

from collections import defaultdict, Counter

# make a 'histogram'/count of all the intervals between repeated values
def hist_intervals(a):
    values = sorted(set(a))  # get list of which values are in a

    # setup the dict to hold the histogram
    hist, last_index = defaultdict(Counter), {}

    # now go through the array and find intervals
    for i, val in enumerate(a):
        if val in last_index
            interval = i - last_index[val]
            hist[val].update((interval,))
        last_index[val] = i
    return hist

这会更快,因为if是用C写的,而且会更干净。你知道吗

您可以通过仔细构造^{}来消除设置循环。然后你只需要对输入列表进行一次扫描,就可以了。在这里,我将结果defaultdict改回常规的Dict[int, Dict[int, int]],但这只是为了让它打印得很好。你知道吗

from collections import defaultdict

def count_intervals(iterable):
    # setup

    last_seen = {}
    hist = defaultdict(lambda: defaultdict(int))

    # The actual work
    for i, x in enumerate(iterable):
        if x in last_seen:
            hist[x][i-last_seen[x]] += 1
        last_seen[x] = i

    return hist

a = [1,2,3,1,5,3,2,4,2,1,5,3,3,4]

hist = count_intervals(a)
for k, v in hist.items():
    print(k, dict(v))

# 1 {3: 1, 6: 1}
# 3 {3: 1, 6: 1, 1: 1}
# 2 {5: 1, 2: 1}
# 5 {6: 1}
# 4 {6: 1}

相关问题 更多 >