使用list和dict comprehensions生成嵌套字典

时间:2012-11-27 12:31:06

标签: python dictionary python-twitter

我正在尝试使用以下格式创建嵌套字典:

{person1:
         {tweet1 that person1 wrote: times that tweet was retweeted},
         {tweet2 that person1 wrote: times that tweet was retweeted},
 person2:
         {tweet1 that person2 wrote: times that tweet was retweeted},...
 }

我正在尝试从以下数据结构创建它。以下是真实版本的截断版本。

 rt_sources =[u'SaleskyKATU', u'johnfaye', u'@anisabartes']
 retweets = [[], 
  [u'Stay safe #nyc #sandy http://t.co/TisObxxT', u'Stay safe #nyc #sandy http://t.co/TisObxxT',u'Stay safe #nyc #sandy http://t.co/TisObxxT', u'Stay safe #nyc #sandy http://t.co/TisObxxT', u'Stay safe #nyc #sandy http://t.co/TisObxxT', u'Stay safe #nyc #sandy http://t.co/TisObxxT', u'Stay safe #nyc #sandy http://t.co/TisObxxT', u'Stay safe #nyc #sandy http://t.co/TisObxxT', u'Stay safe #nyc #sandy http://t.co/TisObxxT', u'Stay safe #nyc #sandy http://t.co/TisObxxT', u'Stay safe #nyc #sandy http://t.co/TisObxxT', u'Stay safe #nyc #sandy http://t.co/TisObxxT', u'Stay safe #nyc #sandy http://t.co/TisObxxT', u'Stay safe #nyc #sandy http://t.co/TisObxxT', u'Stay safe #nyc #sandy http://t.co/TisObxxT', u'Stay safe #nyc #sandy http://t.co/TisObxxT', u'Stay safe #nyc #sandy http://t.co/TisObxxT', u'Stay safe #nyc #sandy http://t.co/TisObxxT', u'Stay safe #nyc #sandy http://t.co/TisObxxT', u'Stay safe #nyc #sandy http://t.co/TisObxxT', u'Stay safe #nyc #sandy http://t.co/TisObxxT', u'Stay safe #nyc #sandy http://t.co/TisObxxT', u'Stay safe #nyc #sandy http://t.co/TisObxxT', u'Stay safe #nyc #sandy http://t.co/TisObxxT'], []]
 annotated_retweets = {u'Stay safe #nyc #sandy http://t.co/TisObxxT':26}
 ''' 
     Key is a tweet from set(retweets) 
     Value is how frequency of each key in retweets
 '''

 for_Nick = {person:dict(tweet_record,[annotated_tweets[tr] for tr in tweet_record]) 
                                    for person,tweet_record in zip(rt_sources,retweets)}

this SO questionthis one似乎都不适用。

3 个答案:

答案 0 :(得分:1)

似乎“人”和“推文”将成为对象,它们拥有自己的数据函数。您可以通过在类中包装来逻辑地关联此想法。例如:

class tweet(object):
    def __init__(self, text):
        self.text = text
        self.retweets = 0
    def retweet(self):
        self.retweets += 1
    def __repr__(self):
        return "(%i)" % (self.retweets)
    def __hash__(self):
        return hash(self.text)

class person(object):
    def __init__(self, name):
        self.name = name
        self.tweets = dict()

    def __repr__(self):
        return "%s : %s" % (self.name, self.tweets)

    def new_tweet(self, text):
        self.tweets[text] = tweet(text)

    def retweet(self, text):
        self.tweets[text].retweet()

M = person("mac389")
M.new_tweet('foo')
M.new_tweet('bar')
M.retweet('foo')
M.retweet('foo')

print M

会给:

mac389 : {'foo': (2), 'bar': (0)}

这里的优点是双重的。一,与人或推文相关的新数据是以明显的逻辑方式添加的。第二个是你已经创建了一个漂亮的用户界面(即使你是唯一使用它的用户!),从长远来看,这将使生活更轻松。

答案 1 :(得分:0)

明确胜于隐性表示Guido

for_Nick = {}
for person,tweets in zip(rt_sources,retweets):
     if person not in for_Nick:
          for_Nick[person] = {}
          for tweet in list(set(tweets)):
               frequency = annotated_retweets[tweet]
               for_Nick[person][tweet] = frequency
     else: #Somehow person already in dictionary <-- Shouldn't happen
         for tweet in tweets:
             if tweet in for_Nick[person]:
                  current_frequency = for_Nick[person][tweet]
                  incoming_frequency = annotated_retweets[tweet]
                  for_Nick[person][tweet] = current_frequency + incoming_frequency
             else: #Person is already there but he said something new
                frequency = annotated_retweets[tweet]
                for_Nick[person][tweet] = frequency

也许有更优雅的形式。

答案 2 :(得分:0)

这可能是你想要构建的词典理解:

for_Nick = {person: 
               {tr: annotated_retweets[tr]
                for tr in set(tweet_record)} 
            for person, tweet_record in zip(rt_sources,retweets)}

您尝试将键列表和值列表传递给dict构造函数,而构造函数则需要一个列表(或其他可迭代的)键值对。