【问题标题】:Generate nested dictionary with list and dict comprehensions使用列表和字典推导生成嵌套字典
【发布时间】:2012-11-27 12:31:06
【问题描述】:

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

{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 似乎都不适用。

【问题讨论】:

  • 请给出实际的示例数据和实际所需的输出。
  • 为什么在没有关于如何改进问题的建议的情况下投反对票?
  • @JanneKarila 谢谢。我编辑了我的答案以修复SyntaxError
  • 当前代码会发生什么?它会引发异常吗?如果是这样,请包括回溯。如果不是,那会发生什么?由于您没有提供任何示例数据,因此无法测试您的代码。

标签: python dictionary python-twitter


【解决方案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)}

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

【讨论】:

    【解决方案2】:

    显式优于隐式 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
    

    也许还有更优雅的形式。

    【讨论】:

      【解决方案3】:

      这可能是您试图构建的 dict 理解:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-10-08
        • 2019-05-09
        • 1970-01-01
        • 2021-01-23
        • 2020-12-15
        • 1970-01-01
        • 2022-11-22
        相关资源
        最近更新 更多