列表中的重复元组

2024-05-19 01:42:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我真的不知道我的代码中出现了什么错误,导致列表中有重复的元组,所以我可以在这方面获得一些帮助吗

因此,我使用google textsearch api搜索某个地方附近的餐馆,在将它们转换为json后,我得到了以下数据。 enter image description here

我试图从中提取每家餐厅的名称、地址、营业时间、价格水平和评级,并将它们转换为列表中的元组。因此,我编写了以下代码来执行此任务:

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

我用这段代码得到了正确的格式,但由于某种原因它复制了结果,我似乎找不到原因。结果是这样的:

enter image description here

有人能帮忙吗

谢谢

以下是“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}]

Tags: httpscomapinewcontainercodeplacerestaurant
3条回答

您可以通过set实现这一点

thelist = list(set(thelist))

编辑: 有了字典,这将失败

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

在这种情况下,您可以从元组中省略字典,或者将其转换为一个字符串,该字符串稍后可以转换回dict

如果你只运行了几次。如果时间不是那么重要的问题

你可以用

mylist = list(dict.fromkeys(mylist))

删除重复项

您可以使用以下代码来实现它,以使事情更清楚、更快地添加您希望在筛选字典时查找的一组关键字

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

相关问题 更多 >

    热门问题