简体   繁体   中英

how to store multiple values for one key in python

The parameter,allWords, contains two column and thousands of rows. The first column tweet. The second one contains a sentiment( 0 for negative and 4 for positive.

As the bottom code shows I have created two dictionaries(negative & positive) to store the word in the dictionary with their frequency.

if you run the code it shows as it follows:

This is for negative dictionary {'transit': 1, 'infect': 4, 'spam': 6}

This is for positive dictionary {'transit': 3, 'infect': 5, 'spam': 2}

   def vectorRepresentation(allWords):       
    negative = {}
    positive = {}

    for (t,s) in allWords:
        if(s=='0'):
            for w in t:
                if w in negative:
                    negative[w]+=1
                else:
                    negative[w]=1
        if(s=='4'):
            for w in t:
                if w in positive:
                    positive[w]+=1
                else:
                    positive[w]=1
     print(negative)
     print(positive)

However, I want to create one dictionary and store the two values for the same key. For example

newDictionary = {'transit': [1][3], 'infect': [4][5], 'spam': [6][2]}

The first value represents the negative. While, the second value is for positive. How can achieve that?

I was going to comment but cannot do that yet so I put it in an answer:

The first answer here might help you achieve what you want:

append multiple values for one key in Python dictionary

In short: You do not need to use numbers for keys, you can also use arrays, so you end up with:

 newDictionary = {'transit': [1,3], 'infect': [4,5], 'spam': [6,2]}

As I think the structure you want is weird and dont make sense , I put them both in one list :

neg = {'transit': 1, 'infect': 4, 'spam': 6}
pos =  {'transit': 3, 'infect': 5, 'spam': 2}
result = {}
for k,v in neg.items():
    result[k] = [v,pos[k]]
result # {'spam': [6, 2], 'transit': [1, 3], 'infect': [4, 5]}

You could make the value for each key be it's own dictionary that had a negative and positive key. So, your modified dictionary would be

{'transit': {'negative': 1, 'positive': 3}} 

So on and so forth.

Or, you could make a little class that stored a negative and positive value and just have that be the value for each of your keys. If your class looked like:

class NegativePositiveStore:
    def __init__(self):
        self.negative = 0
        self.positive = 0

Your values would then all be separate instances of that object. You'd do that like:

word_dict = {}
for (t,s) in allWords:
    for w in t:
        if w in word_dict:
            if (s == '0'):
                word_dict[w].negative += 1
            elif (s == '4'):
                word_dict[w].positive += 1
        else:
            word_dict[w] = NegativePositiveStore()

print(word_dict)

Just keep a pair of int as a value for each key. A defaultdict will help you get rid of some of the bumpiness:

from collections import defaultdict

def vector_representation(all_words):
    neg, pos = 0, 1
    neg_pos = defaultdict(lambda: [0, 0])  # store two values for each key

    for (t, s) in all_words:
        if (s == '0'):
            for w in t:
                neg_pos[w][neg] += 1
        if (s == '4'):
            for w in t:
                neg_pos[w][pos] += 1
    return neg_pos

d = vector_representation(...)

d['transit']
>>> [1, 3] 

d['infect']
>>> [4, 5]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM