python - 如何编写一个函数通过在 python 中清理来缩短我的列表

标签 python

所以我们可以说我有一个大数据,看起来像这样

bigData = [
    {
        "name": "item 1"
    },
    {
        "name": "item 1"
    },
    {
        "name": "item 2"
    },
    {
        "name": "item 2"
    },
    {
        "name": "item 3"
    },
    {
        "name": "item 3"
    },
    {
        "name": "item 3"
    }
]

我正在尝试编写一个函数,将数据最小化为这样的内容

cleanedBigData = [
    {
        "name": "item 1",
        "quantity": 2
    },
    {
        "name": "item 2",
        "quantity": 2
    },
    {
        "name": "item 3",
        "quantity": 3
    }
]

例如,如果尝试过类似的东西

cleanedBigData =  []


for i in bigData:

    for u in cleanedBigData:

        if i["name"] == u["name"]:

            i["quantity"] += 1
        
        else:

            cleaned_data = {
                "name": i.name,
                "quantity": 1
            }

            cleanedBigData.append(cleaned_data)

但它不起作用,可能是因为数组首先是空的,所以第二个 for 循环没有被执行

你会如何解决这个问题?

非常感谢!

最佳答案

对于Pythonic解决方案,您可以使用collections.Counter ,像这样:

from collections import Counter
c = Counter(d['name'] for d in bigData)
cleanedBigData = [{"name": name, "quantity": quantity} for name, quantity in c.items()]

为了获得学习机会,这里有一个基于您的原始代码的解决方案:

cleanedBigData = []

for i in bigData:
    foundInClean = False # We need to "remember" if we found our element
    for u in cleanedBigData:
        if i["name"] == u["name"]:
            foundInClean = True # found it!
            u["quantity"] += 1
    if not foundInClean: # didn't find it, create new one and append it
        cleaned_data = {
            "name": i["name"],
            "quantity": 1
        }
        cleanedBigData.append(cleaned_data)

关于python - 如何编写一个函数通过在 python 中清理来缩短我的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70061870/

相关文章:

python - 如何使用 SEC 网站的 BeautifulSoup 的 getText() 方法忽略 HTML 中嵌入的 jpeg 图像数据

python - 如何使用 ZPT 检查它是否是 Plone 网站中的主页?

python - 使用subprocess.popen SSH,无法输入密码

python - Django 错误 : OperationalError: no such table: polls_poll

python - 如何在 OpenCV(Python)中将灰度图像转换为 RGB?

python - 调用和使用存储在变量中的属性(使用 Beautifulsoup 4)

python - Tensorflow 中的多个分类输入变量

python - Bitbake 服务器无法在适用于 Linux 的 Windows 子系统上启动

python - python中使用networkX的多向图

python - 除了在尝试了try block 和程序的其余部分之后总是抛出 block 之外?