1

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?

Daniel
  • 478
  • 3
  • 16
  • 32
  • 1
    Can you please revise the structure of your expected output. As is, right now it does not make much sense. It's syntactically incorrect to do this: `[1][3]` for how you are looking to store it in your dictionary. Are you looking to do this, maybe: `[1, 3]`? – idjaw Mar 07 '16 at 20:47
  • Short answer: The value of each key in the dictionary will be _something_ that can contain all the information you want to track. It could be a `dict`,`list`, `tuple`, `defaultdict`, `namedtuple`, object of custom class, ... you name it. What to choose depends on how much you'll be doing with your date. Really you should learn about all of these, in this order, and use them as they suit your needs. – alexis Mar 08 '16 at 12:35

4 Answers4

1

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]}
Community
  • 1
  • 1
Valjean
  • 129
  • 8
  • This is a perfectly good answer, why did you want to post it as a comment? – alexis Mar 08 '16 at 12:31
  • because it was posted before to the question I linked by somebody else, so the person who answered the linked question should get the credit? – Valjean Mar 08 '16 at 21:44
1

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]}
ᴀʀᴍᴀɴ
  • 4,443
  • 8
  • 37
  • 57
1

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)
gr1zzly be4r
  • 2,072
  • 1
  • 18
  • 33
0

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]
user2390182
  • 72,016
  • 6
  • 67
  • 89