gpt4 book ai didi

python - 列表中的重复元组

转载 作者:行者123 更新时间:2023-12-04 12:30:39 27 4
gpt4 key购买 nike

请问我能得到一些帮助吗,因为我真的不知道我的代码出了什么问题导致列表有重复的元组。

所以我使用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}]

最佳答案

如果您只运行几次。如果时间不是那么重要的话。

你可以使用,

mylist = list(dict.fromkeys(mylist))

删除重复项。

关于python - 列表中的重复元组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69324189/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com