BenF97 BenF97 - 1 month ago 5
JSON Question

Extracting information depending on user input from a dictionary

I have the following information from a .json loaded and stored in a variable

Data
:

Data = [
{u'rating': u'89', u'rare': u'1', u'name': u'Pogba', u'club_image': u'/content/fifa17/img/clubs/11.png', u'image': u'/content/fifa17/img/players/195864.png', u'rare_type': u'3', u'version': u'IF', u'full_name': u'Pogba', u'position': u'CDM', u'id': u'15073', u'nation_image': u'/content/fifa17/img/nation/18.png'},
{u'rating': u'89', u'rare': u'1', u'name': u'Pogba', u'club_image': u'/content/fifa17/img/clubs/11.png', u'image': u'/content/fifa17/img/players/195864.png', u'rare_type': u'21', u'version': u'OTW', u'full_name': u'Pogba', u'position': u'CM', u'id': u'15091', u'nation_image': u'/content/fifa17/img/nation/18.png'},
{u'rating': u'88', u'rare': u'1', u'name': u'Pogba', u'club_image': u'/content/fifa17/img/clubs/11.png', u'image': u'/content/fifa17/img/players/195864.png', u'rare_type': u'1', u'version': u'Normal', u'full_name': u'Pogba', u'position': u'CM', u'id': u'78', u'nation_image': u'/content/fifa17/img/nation/18.png'}
]


I'd like to extract the information depending on the
rating
of the player inputted by the user, only if another rating exists. EDIT : By this, I mean that if only one line was present, i.e. 1 rating, there would be no need to carry out the process.

For example, if the user inputted:
88
, it would return/print:

{u'rating': u'88', u'rare': u'1', u'name': u'Pogba', u'club_image': u'/content/fifa17/img/clubs/11.png', u'image': u'/content/fifa17/img/players/195864.png', u'rare_type': u'1', u'version': u'Normal', u'full_name': u'Pogba', u'position': u'CM', u'id': u'78', u'nation_image': u'/content/fifa17/img/nation/18.png'}


From my minimal knowledge, I am aware that I must use dictionaries, but am unaware of how to do so. However, I am currently in the following stage in my attempts:

player_dict = {Data}
player_info = player_dict.get(user_input)
if player_info:
for item in player_info:
player_info = item


^ This doesn't seem to work at all.

Answer

The other answers don't include your requirement that data should only be returned if the rating appears in Data once only.

# First iterate over Data and count how many times that rating appears
counter = {}
for item in Data:
    rating = item['rating']
    counter[rating] = counter.get(rating, 0) + 1

# Now build a dict that uses rating as a key and the whole dataset with  
# that rating as a value. Include only ratings that appeared once in Data
cleaned_dict = {item['rating']: item for item in Data if counter.get(item['rating']) == 1}

query = raw_input('Enter rating:')

if cleaned_dict.get(query):
    print "FOUND"
    print cleaned_dict.get(query)
else:
    print "Not found, no need to process further"

Explanation:
In the first for loop, we build a dictionary of {rating: frequency}. The line counter[rating] = counter.get(rating, 0) + 1 says that if the rating isn't in the dictionary, give that rating a default count of 0 and immediately add 1 to it. i.e. it appears once. If, however, later in the for loop we find that rating again, we get the value stored against that rating and add 1 to it. You could also use the Counter object for this but I've found that usually to be slower to get().

In cleaned_dict we use a dictionary comprehension to build a new set of data only if that rating appeared once in Data. We store the whole raw data against a key of just the rating. This allows fast lookup on user input.

Finally, we take user input and look for the rating in our dictionary. If the rating is not a key in cleaned_data then we get None, which is falsey, so if cleaned_dict.get(query): is interpreted as False.

Comments