can I please get some help on this as I really don't know what went wrong in my code that cause the list to have duplicated tuple.
So I am using the google textsearch api to search restaurants near a certain place, and I have the following data after I turned them into json.
And I am trying to extract the name, formatted_address, opening_hours,price_level, and rating of each restaurant from there and turn them into a tuple within a list. So I wrote the following code to perform the job:
for container in l:
for key, value in container.items():
response_data.extend([(container.get('name'),container.get('formatted_address'),container.get('opening_hours'),container.get('price_level'),container.get('rating'))])
I got the right format with this code but it duplicated the result for some reason, and I can't seem to find the reason for that. This is what the result looks like:
Can anyone please help on this?
THANKS!
The following is the sample of the "l":
[{'business_status': 'OPERATIONAL', 'formatted_address': '2893 Broadway, New York, NY 10025, United States', 'geometry': {'location': {'lat': 40.8059222, 'lng': -73.9657833}, 'viewport': {'northeast': {'lat': 40.80723837989272, 'lng': -73.96434252010728}, 'southwest': {'lat': 40.80453872010728, 'lng': -73.96704217989272}}}, 'icon': 'https://maps.gstatic.com/mapfiles/place_api/icons/v1/png_71/restaurant-71.png', 'icon_background_color': '#FF9E67', 'icon_mask_base_uri': 'https://maps.gstatic.com/mapfiles/place_api/icons/v2/restaurant_pinlet', 'name': 'Community Food & Juice', 'opening_hours': {'open_now': False}, 'photos': [{'height': 3024, 'html_attributions': ['<a href="https://maps.google.com/maps/contrib/102912697363536249196">Pavel Samsonov</a>'], 'photo_reference': 'Aap_uECDVWbCOByEhWR_bnBQ8uw76sODCQtKD3CDDSLN09E49FBkN0jXkkp1BeBqU56GNysSjujxzxNp7lSyl0XRfXJB_vjSSi337mZPdL09Lfmt8onfaoHSP4SwpgRsw4-wsKBWPblophhXi90tW-9xQU9fqW2-EAEILuw8oJxvzBrot7yn', 'width': 4032}], 'place_id': 'ChIJnSR09Dv2wokRz_B9gF6v96c', 'plus_code': {'compound_code': 'R24M 9M New York, USA', 'global_code': '87G8R24M 9M'}, 'price_level': 2, 'rating': 4.3, 'reference': 'ChIJnSR09Dv2wokRz_B9gF6v96c', 'types': ['restaurant', 'food', 'point_of_interest', 'establishment'], 'user_ratings_total': 856}, {'business_status': 'CLOSED_TEMPORARILY', 'formatted_address': '1172 Amsterdam Ave, New York, NY 10027, United States', 'geometry': {'location': {'lat': 40.8082935, 'lng': -73.96095}, 'viewport': {'northeast': {'lat': 40.80948682989272, 'lng': -73.95922957010728}, 'southwest': {'lat': 40.80678717010728, 'lng': -73.96192922989273}}}, 'icon': 'https://maps.gstatic.com/mapfiles/place_api/icons/v1/png_71/cafe-71.png', 'icon_background_color': '#FF9E67', 'icon_mask_base_uri': 'https://maps.gstatic.com/mapfiles/place_api/icons/v2/cafe_pinlet', 'name': "Brownie's Cafe", 'permanently_closed': True, 'photos': [{'height': 3024, 'html_attributions': ['<a href="https://maps.google.com/maps/contrib/112203837045938855079">Kevin Jarrett</a>'], 'photo_reference': 'Aap_uEAzXksqRUAzyXeUicnusiczSOUuV-6CxfkIarx-pJEpk1H33Z4IimRRBWwxE5WFrRfqGdKLcPhNYoi4UA1ynIkt2OSOyCBI_I9pqbtZOpf_pyXQ03ayIvj4bQLiIqcYFgPzOeGLefwA25JPvZKABUwtR5xLCNZwPEfRTUz1Hn7vsiUi', 'width': 4032}], 'place_id': 'ChIJ0S64FT72wokRdCj3KIyU4Fk', 'plus_code': {'compound_code': 'R25Q 8J New York, USA', 'global_code': '87G8R25Q 8J'}, 'price_level': 1, 'rating': 4.6, 'reference': 'ChIJ0S64FT72wokRdCj3KIyU4Fk', 'types': ['cafe', 'restaurant', 'food', 'point_of_interest', 'store', 'establishment'], 'user_ratings_total': 53}, {'business_status': 'OPERATIONAL', 'formatted_address': '1239 Amsterdam Ave, New York, NY 10027, United States', 'geometry': {'location': {'lat': 40.809733, 'lng': -73.958608}, 'viewport': {'northeast': {'lat': 40.81118392989273, 'lng': -73.95723977010728}, 'southwest': {'lat': 40.80848427010728, 'lng': -73.95993942989271}}}, 'icon': 'https://maps.gstatic.com/mapfiles/place_api/icons/v1/png_71/restaurant-71.png', 'icon_background_color': '#FF9E67', 'icon_mask_base_uri': 'https://maps.gstatic.com/mapfiles/place_api/icons/v2/restaurant_pinlet', 'name': 'Massawa', 'opening_hours': {'open_now': False}, 'photos': [{'height': 3840, 'html_attributions': ['<a href="https://maps.google.com/maps/contrib/108900354502421745946">Saumya Shah</a>'], 'photo_reference': 'Aap_uEBK6JSZ_Tp3hsfEcKFKtQaXJD2jKspDfuVWSvwzuPp-FfdmNgnHpV_64cJ1e1Wkv58PTce4nv4kKLY-6XGUO_JqAtyWRrIZnU6EW0R9izk0QGz3M82-NfRyh1ZEfGaVxa09dZ6tiS_0lx4KwHDuUWhg6tQmnXVg6MvnV1RFXKzsPoNt', 'width': 2160}], 'place_id': 'ChIJizsMQxX2wokRSD5_pzmhaYg', 'plus_code': {'compound_code': 'R25R VH New York, USA', 'global_code': '87G8R25R VH'}, 'price_level': 2, 'rating': 4.5, 'reference': 'ChIJizsMQxX2wokRSD5_pzmhaYg', 'types': ['restaurant', 'food', 'point_of_interest', 'establishment'], 'user_ratings_total': 361}]
CodePudding user response:
If you are running this only a couple times. And if time is not that much of a issue.
You could just use,
mylist = list(dict.fromkeys(mylist))
To remove the duplicates.
CodePudding user response:
You can achieve this with set
.
thelist = list(set(thelist))
EDIT: With a dictionary this will fail.
>>> l = [(1, "foo"), (1, "foo"), (2, "bar")]
>>> list(set(l))
[(2, 'bar'), (1, 'foo')]
>>> l = [(1, "foo"), (1, "foo"), (2, "bar"), (3, {"open_now": False}), (4, {"open_now": True}), (3, {"open_now": False})]
>>> list(set(l))
TypeError: unhashable type: 'dict'
In this case you can either omit the dictionary from the tuple, or transform it to a string that can be later transformed back to dict
.
CodePudding user response:
You can achieve it using the following code, to make things clearer and faster add the keys that you wish to check into a set which you look up when filtering the dictionary.
In [9]: keys = {"name", "formatted_address", "opening_hours", "price_level", "rating"}
In [10]: [(key,value) for restaurant in l for key,value in restaurant.items() if key in keys]
Out[10]:
[('formatted_address', '2893 Broadway, New York, NY 10025, United States'),
('name', 'Community Food & Juice'),
('opening_hours', {'open_now': False}),
('price_level', 2),
('rating', 4.3),
('formatted_address',
'1172 Amsterdam Ave, New York, NY 10027, United States'),
('name', "Brownie's Cafe"),
('price_level', 1),
('rating', 4.6),
('formatted_address',
'1239 Amsterdam Ave, New York, NY 10027, United States'),
('name', 'Massawa'),
('opening_hours', {'open_now': False}),
('price_level', 2),
('rating', 4.5)]
CodePudding user response:
try this:you data is duplicated because of your second for loop.you do not actually need it.
response_data = []
for container in l:
response_data.append((container.get('name'),container.get('formatted_address'),container.get('opening_hours'),container.get('price_level'),container.get('rating')))