列表中的重复元组
Duplicated tuple within a list
我能得到一些帮助吗,因为我真的不知道我的代码出了什么问题导致列表有重复的元组。
所以我使用google文本搜索api搜索某个地方附近的餐馆,将它们转换为json后得到以下数据。
我正在尝试从那里提取每家餐厅的名称、formatted_address、opening_hours、price_level 和评级,并将它们转换为列表中的元组。所以我写了下面的代码来执行这个工作:
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'))])
我用这段代码得到了正确的格式,但由于某种原因它重复了结果,我似乎找不到原因。结果如下:
任何人都可以帮忙吗?
谢谢!
以下是“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))
删除重复项。
您可以通过 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
。
尝试 this:you 数据重复,因为你的第二个 loop.you 实际上并不需要它。
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')))
您可以使用以下代码实现它,使事情变得更清楚、更快速,将您希望检查的键添加到您在过滤字典时查找的集合中。
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)]
我能得到一些帮助吗,因为我真的不知道我的代码出了什么问题导致列表有重复的元组。
所以我使用google文本搜索api搜索某个地方附近的餐馆,将它们转换为json后得到以下数据。
我正在尝试从那里提取每家餐厅的名称、formatted_address、opening_hours、price_level 和评级,并将它们转换为列表中的元组。所以我写了下面的代码来执行这个工作:
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'))])
我用这段代码得到了正确的格式,但由于某种原因它重复了结果,我似乎找不到原因。结果如下:
任何人都可以帮忙吗?
谢谢!
以下是“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))
删除重复项。
您可以通过 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
。
尝试 this:you 数据重复,因为你的第二个 loop.you 实际上并不需要它。
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')))
您可以使用以下代码实现它,使事情变得更清楚、更快速,将您希望检查的键添加到您在过滤字典时查找的集合中。
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)]