【发布时间】:2016-03-02 17:31:43
【问题描述】:
我是 Python 新手,并试图找出一种相当简单的方法来计算已定义函数的输出。我想通过定义一个函数来计算回复给定用户名的唯一用户数。
st='@'
en=' '
task1dict={}
for t in a,b,c,d,e,f,g,h,i,j,k,l,m,n:
if t['text'][0]=='@':
print('...'),print(t['user']),print(t['text'].split(st)[-1].split(en)[0])
user=t['user']
repliedto=t['text'].split(st)[-1].split(en)[0]
task1dict.setdefault(user, set())
task1dict[user].add(repliedto)
task1dict['realDonaldTrump'].add('joeclarkphd')
这会在我输入时返回下面的内容
print(task1dict)
{'datageek88': {'fundevil', 'joeclarknet', 'joeclarkphd'},
'fundevil': {'datageek88'},
'joeclarkphd': {'datageek88'},
'realDonaldTrump': {'datageek88', 'joeclarkphd'},
'sundevil1992': {'datageek88', 'joeclarkphd'}}
然后我想打印所有回复某个用户的 Twitter 用户,例如,所有回复 datageek88 的人都是由
def print_users_who_got_replies_from(tweeter):
for z in task1dict:
if tweeter in task1dict[z]:
print(z)
这会在我输入时打印出下面的内容:
print_users_who_got_replies_from('datageek88')
fundevil
joeclarkphd
sundevil1992
realDonaldTrump
现在,我想通过定义一个函数来计算回复的数量,然后打印有多少人回复了用户。这个函数应该以数字 (4) 的形式返回答案,但我似乎无法让这部分工作,有什么建议或帮助吗?谢谢!我尝试过使用 len() 函数,但似乎无法让它工作,尽管它可能是答案。
【问题讨论】:
标签: python function dictionary output