分组并计算python或js中的项目数

2024-05-15 02:14:41 发布

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

鉴于这一目标:

 {
    "script": "Georgian",
    "id": 7,
    "script_family": "European",
    "direction": "LTR",
    "num_languages": 11,
    "type": "Alphabet",
    "date": 500,
    "Continent": ""
  },
  {
    "script": "Armenian",
    "id": 8,
    "script_family": "European",
    "direction": "RTL",
    "num_languages": 1,
    "type": "Alphabet",
    "date": 500,
    "Continent": ""
  },
  {
    "script": "Tamil",
    "id": 9,
    "script_family": "Indic",
    "direction": "LTR",
    "num_languages": 6,
    "type": "Syllabary",
    "date": 800,
    "Continent": ""
  },
  {
    "script": "Tibetan",
    "id": 10,
    "script_family": "Central Asian",
    "direction": "LTR",
    "num_languages": 45,
    "type": "Abugida",
    "date": 800,
    "Continent": ""
  },
  {
    "script": "Khmer",
    "id": 11,
    "script_family": "Mainland Southeast Asian",
    "direction": "LTR",
    "num_languages": 3,
    "type": "Abugida",
    "date": 900,
    "Continent": ""
  },

我想创建一个如下所示的对象数组,其中按日期分组,并包含每个脚本系列在该日期出现的脚本数

data = [
{date: 500, European: 2}
{date: 800, Indic: 1, Central Asia: 1}
...
]

其中,有时一个数据可以有多个脚本族

我尝试了以下代码: family = data.groupby(['date', 'script_family'])['script_family'].count() 但当我将其导出为csv时,我只得到“脚本族”的计数,尽管我希望在特定日期出现的每个脚本族都设置为脚本数

date   script_family           
-400   European                    1
-300   East Asian                  1
-200   Middle Eastern              1
-100   European                    1
 500   African                     1
       European                    2
 600   Middle Eastern              1
 800   Central Asian               1
       Indic                       1
 900   East Asian                  1
       European                    1
       Indic                       3
       Mainland Southeast Asian    1
 1000  Indic                       1
 1100  Indic                       2
       Mainland Southeast Asian    1
 1200  Indic                       1
 1300  Central Asian               1
       Mainland Southeast Asian    1
...

Tags: 脚本iddatetypescriptfamilynumcentral
1条回答
网友
1楼 · 发布于 2024-05-15 02:14:41

适用于Python 2.7.18和3.9.1:

from collections import Counter
from itertools import groupby
from operator import itemgetter

data = ...  # load the data
data = sorted(data, key=itemgetter('date'))  # groupby needs sorted data

results = [
    dict(
        date=date,
        **(Counter(map(itemgetter('script_family'), dated_scripts)))) 
        for date, dated_scripts in groupby(data, key=itemgetter('date')
    )
]
print(results)

参考:

相关问题 更多 >

    热门问题