I'm trying to create a nested dictionary with the following format:
{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},...
}
I'm trying to create it from the following data structures. The following are truncated versions of the real ones.
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)}
Neither this SO question nor this one seem to apply.
It seems that a "person" and a "tweet" are going to be objects that have their own data, and functions. You can logically associate this idea by wrapping things up in a class. For example:
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
Would give:
mac389 : {'foo': (2), 'bar': (0)}
The advantage here is twofold. One, is that new data associated with a person or tweet is added in an obvious and logical way. The second is that you've created a nice user interface (even if you're the only one using it!) that will make life easier in the long run.