简体   繁体   English

如何在python中存储一个键的多个值

[英]how to store multiple values for one key in python

The parameter,allWords, contains two column and thousands of rows. 参数allWords包含两列和数千行。 The first column tweet. 第一栏推文。 The second one contains a sentiment( 0 for negative and 4 for positive. 第二个包含情绪(0表示负数,4表示正数。

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} 这是负字典{'transit':1,'infect':4,'spam':6}

This is for positive dictionary {'transit': 3, 'infect': 5, 'spam': 2} 这是针对正字典{'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]} 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 在Python字典中为一个键附加多个值

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. 您可以为每个键创建值,使其具有negative键和positive键的字典。 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. 只需保留一对int作为每个键的值。 A defaultdict will help you get rid of some of the bumpiness: defaultdict将帮助您摆脱一些颠簸:

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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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