gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 04:40:55 24 4
gpt4 key购买 nike

我想制作列表中重复值之间所有间隔的直方图。我写了一些有效的代码,但它使用了带有 if 语句的 for 循环。我经常发现,如果可以设法使用巧妙的切片和/或预定义的 python (numpy) 方法编写一个版本,那么可以获得比使用 for 循环快得多的 Python 代码,但在这种情况下我想不出任何方法这样做。任何人都可以建议一种更快或更 pythonic 的方法吗?

# 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}}

^ 注意:我不关心字典中的顺序,所以这个解决方案是可以接受的,但我希望能够在真的大型数组/列表上运行,我怀疑我目前的方法会很慢。

最佳答案

您可以通过精心构造的 defaultdict 消除设置循环.然后,您只需对输入列表进行一次扫描,就可以了。在这里,我将生成的 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}

关于python - 快速/Pythonic 方法来计算重复列表值之间的间隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50663079/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com