1

I'm new to Python and trying to figure out a rather simple way to count the output of a defined function. I want to count the number of unique users who have replied to a given username by defining a function to do this.

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')

This returns what is below when I enter

print(task1dict)

{'datageek88': {'fundevil', 'joeclarknet', 'joeclarkphd'},
 'fundevil': {'datageek88'},
 'joeclarkphd': {'datageek88'},
 'realDonaldTrump': {'datageek88', 'joeclarkphd'},
 'sundevil1992': {'datageek88', 'joeclarkphd'}}

I then want to print all the Twitter users who replied to a certain user for example, all the people who replied to datageek88 is done by

def print_users_who_got_replies_from(tweeter):
    for z in task1dict:
        if tweeter in task1dict[z]:
            print(z)

This prints me what is below when I enter:

print_users_who_got_replies_from('datageek88')

fundevil
joeclarkphd
sundevil1992
realDonaldTrump

Now, I want to count the number of replies by defining a function that then prints how many people replied to a user. This function should return the answer as a number (4), but I can't seem to get that part to work, any suggestions or help? Thanks! I have tried using the len() function but can't seem to get that to work, although it might be the answer.

1 Answer 1

1

Rule of thumb: when you have a function that prints many things, and you think "ok now how do I interact with those values that were printed?", that's a signal that you should be appending those values to a list rather than printing them.

In this case, the most straightforward modification to the code would be

def get_users_who_got_replies_from(tweeter):
    result = []
    for z in task1dict:
        if tweeter in task1dict[z]:
            result.append(z)
    return result

seq = get_users_who_got_replies_from('datageek88')
for item in seq:
    print(item)
print("Number of users who got replies:", len(seq))

Bonus advanced approach: strictly speaking, you don't need a whole function just to create and return one list based on the contents of another iterable. You could do it with a list comprehension:

seq = [z for z in task1dict if 'datageek88' in task1dict[x]]
for item in seq:
    print(item)
print("Number of users who got replies:", len(seq))
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! This helped me so much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.