我有一本这样的字典:

{4722: "['children', 'dance', 'education','technology', 'teaching']",
3200: "['alternative energy', 'sustainability', 'technology']",
1636: "['computers', 'performance', 'simplicity', 'software', 'ice']",
1697: "['MacArthur grant', 'inequality', 'technology', 'pollution']"}


现在,我想在每一行中找到“技术”一词,并对键求和。像这里一样,我的总和为4722 + 3200 + 1697。

谁能帮我吗?

我应该提到我的原始数据框有2000行。

最佳答案

your_data = {
    4722: "['children', 'dance', 'education','technology', 'teaching']",
    3200: "['alternative energy', 'sustainability', 'technology']",
    1636: "['computers', 'performance', 'simplicity', 'software', 'ice']",
    1697: "['MacArthur grant', 'inequality', 'technology', 'pollution']"
}

sum_up = 0
for k, v in your_data.items():
    if 'technology' in v:
        sum_up += k

print('sum_up:', sum_up)

关于python - 如何遍历字典中每个键具有多个值的值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47228622/

10-12 19:21