DataSolve 2: How to compare retrieved values from a Python dictionary and print comparison results
So, this is not necessarily data related but I'll just file this under DataSolve series. This will be the second installment.
Basically, DataSolve willl be anything that I saw online and decided to answer. We are looking into a question posted by a member of a local Python group (PH) I'm a member of.
The question goes:
Hi. sa mga veterans natin dyan.
paano ko ico-compare yung dictionary ko kung si A ay greater than B?
e.g.
import random
data = [
{
'team': 'example_user_1',
'follower_count': 346,
},
{
'team': 'example_user_2',
'follower_count': 215,
},
{ 'team': 'example_user_3',
'follower_count': 500,
},
{ 'team': 'example_user_4',
'follower_count': 600,
},
]
random_data = random.choice(data)
# supposed to compare the data in the dictionary
print(f" Compare A: {random_data['team']}")
print(f" Compare B: {random_data['team']}")
kasi ang nangyayari same ng data kay compare A and compare B
example output.
Compare A: example_user_1
Compare B: example_user_2
btw newbie po.
thank you.It's in Taglish but I think the user's question is how to compare the contents of a randomly picked item from a dictionary to another randomly picked item in the same dictionary, then output a comparison between them.
The problem with their code is it's incomplete. There's no comparison being made and it looks like it's referencing the same variable and not even accessing the item/s inside the randomly chosen data.
Here's how I might modify it:
import random
data = [
{
'team': 'example_user_1',
'follower_count': 346,
},
{
'team': 'example_user_2',
'follower_count': 215,
},
{'team': 'example_user_3',
'follower_count': 500,
},
{'team': 'example_user_4',
'follower_count': 600,
},
]
random_pick_1 = random.choice(data)
r1_team = random_pick_1['team']
r1_fcount = random_pick_1['follower_count']
random_pick_2 = random.choice(data)
r2_team = random_pick_2['team']
r2_fcount = random_pick_2['follower_count']
if r1_fcount > r2_fcount:
print(f"{r1_team} has more followers than {r2_team}: {r1_fcount} vs {r2_fcount}")
elif r1_fcount < r2_fcount:
print(f"{r2_team} has more followers than {r1_team}: {r2_fcount} vs {r1_fcount}")
elif r1_fcount == r2_fcount:
if r1_team > r2_team:
print(f"Same item picked by random picker: {r1_team}: {r1_fcount}")
else:
print(
f"{r1_team} has same follower count with {r2_team}: {r1_fcount} vs {r2_fcount}")
So the idea is to pick a random item from the dictionary and assign it to our first variable, access the items on that dictionary and give them variables as well. Do the same thing to a second variable.
This assumes that the items inside the dictionary are all the same formatting etc.
An elif ladder with a nest is used for logical comparison and will print out the team, along with follower count and comparison result of two variables.
I'm assuming you're not a complete newbie so the explanation and idea is short.
That's it, see you on the next entry.

Comments
Post a Comment