I'd like to compare and integrate dictionary values in Python.

Asked 2 years ago, Updated 2 years ago, 68 views

While performing the Emotional Analysis task, you created a list-dictionary containing individual key values and value values as follows:

tempDict = [{'Entity': 'No', 'Feature': 'GEN', 'Nagative': 1, 'Snagative': 1},
{'Entity': 'No', 'Feature': 'GEN', 'Nagative': 1, 'Snagative': 1},
{'Entity': 'Ro', 'Feature': 'Photo', 'Positive': 1, 'Snagative': 1},
{'Entity': 'Ro', 'Feature': 'Picture', 'Negative':')
{'Entity': 'Ro', 'Feature': 'Camera', 'Negative':')]

In the above state, we would like to integrate the polarity values of elements with the same 'Entity/Feature' value into the following form.

newDict = [{'Entity': 'No', 'Feature': 'GEN', 'Nagative': 2, 'Snagative': 2},
{'Entity': 'Ro', 'Feature': 'Photo', 'Positive': 1, 'Snagative': 1, 'Negative': 1]
{'Entity': 'Ro', 'Feature': 'Camera', 'Negative':')]

We have a poor understanding of dictionaries and are not able to solve them. I'd appreciate it if you could tell me how.

dictionary python

2022-09-22 19:40

2 Answers

>>> tempDict = [{'Entity': 'No', 'Feature': 'GEN', 'Nagative': 1, 'Snagative': 1},
{'Entity': 'No', 'Feature': 'GEN', 'Nagative': 1, 'Snagative': 1},
{'Entity': 'Ro', 'Feature': 'Photo', 'Positive': 1, 'Snagative': 1},
{'Entity': 'Ro', 'Feature': 'Picture', 'Negative': 1},
{'Entity': 'Ro', 'Feature': 'Camera', 'Negative':')]
>>> import pandas as pd

>>> df = pd.DataFrame(tempDict)
>>> df
  Entity Feature  Nagative  SNagative  Positive  Negative
0 No, GEN 1.0 1.0 NaN NaN
1 No, GEN 1.0 1.0 NaN NaN
Photo 2 NaN 1.0 NaN 1.0 NaN
Photo 3. NaN NaN NaN 1.0
Camera 4, NaN NaN NaN 1.0
>>> df = df.fillna(0)
>>> df
  Entity Feature  Nagative  SNagative  Positive  Negative
0 No, GEN 1.0 1.0 0.0 0.0
1 No, GEN 1.0 1.0 0.0 0.0
Picture 2.0.0.01.01.00.0
Picture 3.0.0.00.00.01.0
4 ro camera 0.0 0.0 0.0 1.0 1.0

>>> df1 = df.groupby(['Entity', 'Feature'], as_index=False).sum()
>>> df1
  Entity Feature  Nagative  SNagative  Positive  Negative
Zero picture 0.0 1.0 1.0 1.0 1.0
1 Ro Camera 0.0 0.0 0.0 0.0 1.0
2... No, GEN 2.0, 2.0, 0.0

This is a method of replacing it with a panda data frame and using groupby.


2022-09-22 19:40

I wrote it with only the basic module.

I started it because I thought it would be simple, but there's more to consider than I thought. If it is this long, it is better to write it as a separate function than compression.

tempDict = [{'Entity': 'No', 'Feature': 'GEN', 'Nagative': 1, 'Snagative': 1},
{'Entity': 'No', 'Feature': 'GEN', 'Nagative': 1, 'Snagative': 1},
{'Entity': 'Ro', 'Feature': 'Photo', 'Positive': 1, 'Snagative': 1},
{'Entity': 'Ro', 'Feature': 'Picture', 'Negative': 1},
{'Entity': 'Ro', 'Feature': 'Camera', 'Negative':')]

import itertools as it
from collections import Counter
from functools import reduce

tempdict_grouped = {(('Entity',key[0]), ('Feature', key[1])): 
                        tuple((reduce(lambda x, y: x + y, [Counter({_k: _v for _k, _v in di.items() if _k not in ['Entity', 'Feature']}) for di in group])).items()) 
                            for key, group in it.groupby(tempDict, lambda e:(e['Entity'], e['Feature']))}

for k, v in tempdict_grouped.items():
    print({**dict(k), **dict(v)})
{'Entity': 'No', 'Feature': 'GEN', 'Nagative': 2, 'Snagative': 2'
{'Entity': 'Ro', 'Feature': 'Photo', 'Positive': 1, 'Snagative': 1, 'Negative': 1}
{'Entity': 'Ro', 'Feature': 'Camera', 'Negative':')


2022-09-22 19:40

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.