【问题标题】:Getting Python Output Dictionary with calculations使用计算获取 Python 输出字典
【发布时间】:2022-01-07 11:00:34
【问题描述】:

我是 Python 和 StackOverflow 的新手。我目前正忙于学习 Python Online,我需要完成一个项目,该项目需要 Python 代码的非常具体的输出才能继续该项目。 我被一个问题困了一个多星期,我找不到解决方案。因此,我们非常欢迎任何帮助。

我们来自这些数据(datos_lista):

('23', 'RETIRO', 'Despejado', (25, 29), 0)
('22', 'MONCLOA-ARAVACA', 'Despejado', (21, 24), 6)
('20', 'FUENCARRAL-EL PARDO', 'Despejado', (45, 49), 14)
('20', 'FUENCARRAL-EL PARDO', 'Despejado', (25, 29), 7)
('20', 'FUENCARRAL-EL PARDO', 'Despejado', (25, 29), 4)

enter image description here

我们需要做的是:

  1. datos_lista[3] 按 (AgeRange = (21,24),...) 分组:

按年龄范围分组 ((25, 29),(21, 24),(45, 49),...)

  1. datos_lista[3] 计数:

计算 AgeRange 出现的次数。

  1. 并且,3- datos_lista[4]== Count if == 4:

计算 datos_lista[4]== 4 的次数 (在上面的示例中,这仅在最后一行出现一次) (('20', 'FUENCARRAL-EL PARDO', 'Despejado', (25, 29), 4))

这个问题需要我提供这个输出:

Output required

所有这些都需要在这个“代码检查”上工作:

"""# Prueba de funcionamiento1:

total_accidentes_y_muertes_por_edades = totales_mortales(datos_lista)

for k, totales in total_accidentes_y_muertes_por_edades.items: print(k, totales)
print()

"""# Prueba de funcionamiento2:

tasa_accidentes_mortales_por_mil = [(k, m*1000/n) for k, (n, m) in total_accidentes_y_muertes_por_edades.items()]

for k_tasa  in tasa_accidentes_mortales_por_mil:
print(k_tasa)
                      

"""

我已经能够单独生成代码并匹配解决方案,但是它们都没有通过“代码检查”或一起产生输出。

代码尝试:

1-

def totales (datos_lista):
from collections import Counter
totales1 = Counter()

for p in datos_lista:
    totales1[p[3]] += 1


return(totales1)

2-

def totales2 (datos_lista):

from collections import Counter
from collections import defaultdict

totales2 = Counter()
for q in datos_lista:
    if q [4] == 4:
        totales2[q[3]] += 1

return(totales2)

非常感谢您, (如果有什么不清楚的地方请告诉我,我是新手,所以我不知道如何正确地制定它以使其足够清楚) 丹尼尔

【问题讨论】:

  • 能不能把What we need to do is的这部分描述详细一点,目前的资料第二项和第三项都不清楚。
  • 您好,对此表示歉意...我在其中添加了一些额外的 cmets...希望这些使解释更容易理解我们需要做的是:datos_lista[3] group by ( AgeRange = (21,24),...): 按年龄范围分组 ((25, 29),(21, 24),(45, 49),...) datos_lista[3] count: 统计数量AgeRange 出现的次数。并且, 3- datos_lista[4]== Count if == 4:计算 datos_lista[4]== 4 的次数(在上面的示例中,这仅在最后一行出现一次)(('20',' FUENCARRAL-EL PARDO', 'Despejado', (25, 29), 4))

标签: python counter dictionary-comprehension


【解决方案1】:

你的Prueba de funcionamiento1 代码有问题,total_accidentes_y_muertes_por_edades.items 是一个方法,你没有调用它,你在它上面做了一个 for 循环,所以它会抛出一个异常。

根据你对问题的描述,我定义了函数totales2并得到输出。

datos_lista = [
    ('23', 'RETIRO', 'Despejado', (25, 29), 0),
    ('22', 'MONCLOA-ARAVACA', 'Despejado', (21, 24), 6),
    ('20', 'FUENCARRAL-EL PARDO', 'Despejado', (45, 49), 14),
    ('20', 'FUENCARRAL-EL PARDO', 'Despejado', (25, 29), 7),
    ('20', 'FUENCARRAL-EL PARDO', 'Despejado', (25, 29), 4)
]


def totales2(datos_lista):
    counter = {}
    for *_, age_range, c in datos_lista:
        t = counter.setdefault(age_range, [0, 0])
        t[0] += 1
        if c == 4:
            t[1] += 1
    return counter


total_accidentes_y_muertes_por_edades = totales2(datos_lista)

for k, totales in total_accidentes_y_muertes_por_edades.items():
    print(k, totales)

print()


tasa_accidentes_mortales_por_mil = [(k, m*1000/n) for k, (n, m) in total_accidentes_y_muertes_por_edades.items()]

for k_tasa in tasa_accidentes_mortales_por_mil:
    print(k_tasa)

输出:

(25, 29) [3, 1]
(21, 24) [1, 0]
(45, 49) [1, 0]

((25, 29), 333.3333333333333)
((21, 24), 0.0)
((45, 49), 0.0)

【讨论】:

  • 您好,pppig,非常感谢您提供以上信息!您的方法更简单有效!我也会修改“Prueba de Funcionamiento1”!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-02
  • 1970-01-01
  • 2015-04-18
  • 1970-01-01
  • 2015-01-28
  • 1970-01-01
相关资源
最近更新 更多