Python 生成包含列表和dict理解的嵌套字典
我正在尝试使用以下格式创建嵌套词典:Python 生成包含列表和dict理解的嵌套字典,python,dictionary,python-twitter,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},..
{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)}
两者似乎都不适用 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
_Nick={}
对于个人,使用zip发送推文(rt_来源、转发):
如果此人不在现场,则:
对于_Nick[person]={}
对于列表中的tweet(设置(tweets)):
频率=带注释的_转发[推特]
for_Nick[person][tweet]=频率
否则:#不知何故,字典里已经有人了看来“人”和“推特”将成为拥有自己数据和功能的对象。你可以通过在一个类中包装东西来逻辑地关联这个想法。例如:
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)}
这里的优势是双重的。一是,与某人或推特相关的新数据以一种明显且合乎逻辑的方式添加。第二,你已经创建了一个很好的用户界面(即使你是唯一一个使用它的人!),从长远来看,这将使生活更轻松。这可能是你试图构建的dict理解:
for_Nick = {person:
{tr: annotated_retweets[tr]
for tr in set(tweet_record)}
for person, tweet_record in zip(rt_sources,retweets)}
您试图向
dict
构造函数传递一个键列表和一个值列表,该构造函数需要一个键值对列表(或其他iterable) 请给出实际的示例数据和实际的期望输出。为什么在没有建议我如何改进问题的情况下进行否决投票?@JanneKarila,谢谢。我编辑了我的答案以修复语法错误。当前代码会发生什么?它是否引发了一个例外?如果是,请包括回溯。如果没有,发生了什么?由于您没有提供任何示例数据,因此无法测试代码。