Python 从元组列表创建频率字典

Python 从元组列表创建频率字典,python,list,tuples,histogram,frequency,Python,List,Tuples,Histogram,Frequency,我有 d = [(4, 1), (4, 1), (4, 1), (4, 1), (4, 3), (4, 2), (4, 2), (4, 4), (4, 1), (4, 3), (4, 1), (4, 1), (4, 2), (4, 1)] 但要大很多倍 每个元组中的第一个数字是月份,第二个数字是事件数。我需要计算每个月的事故数量,并编制每个月的事故总数。 到目前为止,我已经: def histogram(L): y = {} for x in L: if x[

我有

d = [(4, 1), (4, 1), (4, 1), (4, 1), (4, 3), (4, 2), (4, 2), (4, 4), (4, 1), (4, 3), (4, 1), (4, 1), (4, 2), (4, 1)] 
但要大很多倍

每个元组中的第一个数字是月份,第二个数字是事件数。我需要计算每个月的事故数量,并编制每个月的事故总数。 到目前为止,我已经:

def histogram(L):
    y = {}
    for x in L:
        if x[0] in y.keys():
            y[x] = y[x] + x[1]
        else:
            y[x] = x[1]
    return y
我需要一个类似的输出 y={4=24}(不必是字典) 但是,对于列表d中的一系列数字,其范围相当广泛

电流输出为

{(4, 2): 2, (4, 4): 4, (4, 1): 1, (4, 3): 3}
感谢您在此处使用dict理解(考虑到数据是按月份排序的):

要改进您的代码,您应该做的第一件事是删除
.keys()
调用(虽然这在这里不太重要,因为我们只能有12个月),因为simple
key in dct
会在O(1)时间内搜索该键。另一个问题是您使用
x
作为键,但您应该使用
x[1]
作为键:

def histogram(L):
    y = {}
    for m, c in L:            #take the advantage of tuple unpacking
        y[m] = y.get(m, 0) + c
如果您确定您总是需要dict中的所有12个月,则首先初始化所有月份:

def histogram(L):
    y = dict.fromkeys(range(1, 13), 0)
    for m, c in L:          
        y[m] += c
您可以在此处使用dict理解(考虑数据按月份排序):

要改进您的代码,您应该做的第一件事是删除
.keys()
调用(虽然这在这里不太重要,因为我们只能有12个月),因为simple
key in dct
会在O(1)时间内搜索该键。另一个问题是您使用
x
作为键,但您应该使用
x[1]
作为键:

def histogram(L):
    y = {}
    for m, c in L:            #take the advantage of tuple unpacking
        y[m] = y.get(m, 0) + c
如果您确定您总是需要dict中的所有12个月,则首先初始化所有月份:

def histogram(L):
    y = dict.fromkeys(range(1, 13), 0)
    for m, c in L:          
        y[m] += c
您可以在此处使用dict理解(考虑数据按月份排序):

要改进您的代码,您应该做的第一件事是删除
.keys()
调用(虽然这在这里不太重要,因为我们只能有12个月),因为simple
key in dct
会在O(1)时间内搜索该键。另一个问题是您使用
x
作为键,但您应该使用
x[1]
作为键:

def histogram(L):
    y = {}
    for m, c in L:            #take the advantage of tuple unpacking
        y[m] = y.get(m, 0) + c
如果您确定您总是需要dict中的所有12个月,则首先初始化所有月份:

def histogram(L):
    y = dict.fromkeys(range(1, 13), 0)
    for m, c in L:          
        y[m] += c
您可以在此处使用dict理解(考虑数据按月份排序):

要改进您的代码,您应该做的第一件事是删除
.keys()
调用(虽然这在这里不太重要,因为我们只能有12个月),因为simple
key in dct
会在O(1)时间内搜索该键。另一个问题是您使用
x
作为键,但您应该使用
x[1]
作为键:

def histogram(L):
    y = {}
    for m, c in L:            #take the advantage of tuple unpacking
        y[m] = y.get(m, 0) + c
如果您确定您总是需要dict中的所有12个月,则首先初始化所有月份:

def histogram(L):
    y = dict.fromkeys(range(1, 13), 0)
    for m, c in L:          
        y[m] += c
你可以用。我还为您的示例添加了一些额外的数据

d = [(4, 1), (4, 1), (4, 1), (4, 1), (4, 3), (4, 2), (4, 2), (4, 4), (4, 1), (4, 3), (4, 1), (4, 1), (4, 2), (4, 1), (5,1), (5,2)]

from collections import Counter

counter = Counter()

for x, y in d:
    counter[x]+=y
然后
counter==计数器({4:49,5:3})

您可以使用。我还为您的示例添加了一些额外的数据

d = [(4, 1), (4, 1), (4, 1), (4, 1), (4, 3), (4, 2), (4, 2), (4, 4), (4, 1), (4, 3), (4, 1), (4, 1), (4, 2), (4, 1), (5,1), (5,2)]

from collections import Counter

counter = Counter()

for x, y in d:
    counter[x]+=y
然后
counter==计数器({4:49,5:3})

您可以使用。我还为您的示例添加了一些额外的数据

d = [(4, 1), (4, 1), (4, 1), (4, 1), (4, 3), (4, 2), (4, 2), (4, 4), (4, 1), (4, 3), (4, 1), (4, 1), (4, 2), (4, 1), (5,1), (5,2)]

from collections import Counter

counter = Counter()

for x, y in d:
    counter[x]+=y
然后
counter==计数器({4:49,5:3})

您可以使用。我还为您的示例添加了一些额外的数据

d = [(4, 1), (4, 1), (4, 1), (4, 1), (4, 3), (4, 2), (4, 2), (4, 4), (4, 1), (4, 3), (4, 1), (4, 1), (4, 2), (4, 1), (5,1), (5,2)]

from collections import Counter

counter = Counter()

for x, y in d:
    counter[x]+=y
然后
counter==计数器({4:49,5:3})

这应该可以解决它

d=[(4,1)、(4,1)、(4,1)、(4,1)、(4,3)、(4,2)、(4,2)、(4,4)、(4,4)、(4,1)、(4,3)、(4,1)、(4,1)、(4,1)、(4,2)、(4,1)]

def直方图(L): y={} 对于L中的t:

month = t[0]
freq = t[1]
try :
  y[month] += freq
except KeyError:
  y[month] = 0
  y[month] += freq
返回y

打印(直方图(d))

这应该可以解决它

d=[(4,1)、(4,1)、(4,1)、(4,1)、(4,3)、(4,2)、(4,2)、(4,4)、(4,4)、(4,1)、(4,3)、(4,1)、(4,1)、(4,1)、(4,2)、(4,1)]

def直方图(L): y={} 对于L中的t:

month = t[0]
freq = t[1]
try :
  y[month] += freq
except KeyError:
  y[month] = 0
  y[month] += freq
返回y

打印(直方图(d))

这应该可以解决它

d=[(4,1)、(4,1)、(4,1)、(4,1)、(4,3)、(4,2)、(4,2)、(4,4)、(4,4)、(4,1)、(4,3)、(4,1)、(4,1)、(4,1)、(4,2)、(4,1)]

def直方图(L): y={} 对于L中的t:

month = t[0]
freq = t[1]
try :
  y[month] += freq
except KeyError:
  y[month] = 0
  y[month] += freq
返回y

打印(直方图(d))

这应该可以解决它

d=[(4,1)、(4,1)、(4,1)、(4,1)、(4,3)、(4,2)、(4,2)、(4,4)、(4,4)、(4,1)、(4,3)、(4,1)、(4,1)、(4,1)、(4,2)、(4,1)]

def直方图(L): y={} 对于L中的t:

month = t[0]
freq = t[1]
try :
  y[month] += freq
except KeyError:
  y[month] = 0
  y[month] += freq
返回y


打印(直方图(d))

我稍微更改了变量的名称

incidents = [(4, 1), (4, 1), (4, 1), (4, 1),
             (4, 3), (4, 2), (4, 2), (4, 4),
             (4, 1), (4, 3), (4, 1), (4, 1),
             (4, 2), (4, 1)]
inc_by_m = {}
for m, n in incidents:
    inc_by_m[m] = inc_by_m.get(m,0)+n
print inc_by_m
# {4:24}

简单的代码基于字典的
.get()
方法的可选参数(此处为
0
),
get
返回强制参数索引的值(如果以前设置过),或者返回可选参数索引的值(如果没有设置)。

我稍微更改了变量的名称

incidents = [(4, 1), (4, 1), (4, 1), (4, 1),
             (4, 3), (4, 2), (4, 2), (4, 4),
             (4, 1), (4, 3), (4, 1), (4, 1),
             (4, 2), (4, 1)]
inc_by_m = {}
for m, n in incidents:
    inc_by_m[m] = inc_by_m.get(m,0)+n
print inc_by_m
# {4:24}

简单的代码基于字典的
.get()
方法的可选参数(此处为
0
),
get
返回强制参数索引的值(如果以前设置过),或者返回可选参数索引的值(如果没有设置)。

我稍微更改了变量的名称

incidents = [(4, 1), (4, 1), (4, 1), (4, 1),
             (4, 3), (4, 2), (4, 2), (4, 4),
             (4, 1), (4, 3), (4, 1), (4, 1),
             (4, 2), (4, 1)]
inc_by_m = {}
for m, n in incidents:
    inc_by_m[m] = inc_by_m.get(m,0)+n
print inc_by_m
# {4:24}

简单的代码基于字典的
.get()
方法的可选参数(此处为
0
),
get
返回强制参数索引的值(如果以前设置过),或者返回可选参数索引的值(如果没有设置)。

我稍微更改了变量的名称

incidents = [(4, 1), (4, 1), (4, 1), (4, 1),
             (4, 3), (4, 2), (4, 2), (4, 4),
             (4, 1), (4, 3), (4, 1), (4, 1),
             (4, 2), (4, 1)]
inc_by_m = {}
for m, n in incidents:
    inc_by_m[m] = inc_by_m.get(m,0)+n
print inc_by_m
# {4:24}


简单的代码是基于字典的
.get()
方法的可选参数(此处为
0
)的,
get
返回强制参数索引的值(如果以前设置过),或者返回可选参数索引的值(如果没有设置)。

您的数据是按月份排序的吗?@AshwiniChaudhary是的,实际上是,从1到12那么您对代码的哪些方面不满意?结果与您想要的有什么不同?@KarlKnechtel它目前不起作用,我不确定问题在输出中编辑的位置,因为它现在是@KarlKnechtelIs您的数据是按月份排序的吗?@AshwiniChaudhary是的,它实际上是从1到12,所以呢