猿问

我如何编写一个函数,将餐厅列表作为参数并返回仅包含未关闭餐厅的列表?

fork_fig = {'categories': [{'alias': 'burgers', 'title': 'Burgers'},

  {'alias': 'sandwiches', 'title': 'Sandwiches'},

  {'alias': 'salad', 'title': 'Salad'}],

 'coordinates': {'latitude': 35.10871, 'longitude': -106.56739},

 'display_phone': '(505) 881-5293',

 'distance': 3571.724649307866,

 'id': 'fork-and-fig-albuquerque',

 'image_url': 'https://s3-media1.fl.yelpcdn.com/bphoto/_-DpXKfS3jv6DyA47g6Fxg/o.jpg',

 'is_closed': False,

 'location': {'address1': '6904 Menaul Blvd NE',

  'address2': 'Ste C',

  'address3': '',

  'city': 'Albuquerque',

  'country': 'US',

  'display_address': ['6904 Menaul Blvd NE', 'Ste C', 'Albuquerque, NM 87110'],

  'state': 'NM',

  'zip_code': '87110'},

 'name': 'Fork & Fig',

 'phone': '+15058815293',

 'price': '$$',

 'rating': 4.5,

 'review_count': 604}


frontier_restaurant = {'categories': [{'alias': 'mexican', 'title': 'Mexican'},

  {'alias': 'diners', 'title': 'Diners'},

  {'alias': 'tradamerican', 'title': 'American (Traditional)'}],

 'coordinates': {'latitude': 35.0808088832532, 'longitude': -106.619402244687},

 'display_phone': '(505) 266-0550',

 'distance': 4033.6583235266075,

 'id': 'frontier-restaurant-albuquerque-2',

 'image_url': 'https://s3-media4.fl.yelpcdn.com/bphoto/M9L2z6-G0NobuDJ6YTh6VA/o.jpg',

 'is_closed': True,

 'location': {'address1': '2400 Central Ave SE',

  'address2': '',

  'address3': '',

  'city': 'Albuquerque',

  'country': 'US',

  'display_address': ['2400 Central Ave SE', 'Albuquerque, NM 87106'],

  'state': 'NM',

  'zip_code': '87106'},

 'name': 'Frontier Restaurant',

 'phone': '+15052660550',

 'price': '$',

 'rating': 4.0,

 'review_count': 1369}

我有两个上面的餐厅列表,我想制作一个函数,该函数只返回一个没有使用条件循环关闭的餐厅的列表。


restaurants = [fork_fig, frontier_restaurant]


open_restaurants(restaurants)[0]['name'] ###I want restaurant name to be appear

下面是我一直在处理的代码,我不太确定如何解决这个问题以获得我想要返回的值。

千万里不及你
浏览 171回答 3
3回答

至尊宝的传说

继续评论:fork_fig = {'categories': [{'alias': 'burgers', 'title': 'Burgers'},  {'alias': 'sandwiches', 'title': 'Sandwiches'},  {'alias': 'salad', 'title': 'Salad'}], 'coordinates': {'latitude': 35.10871, 'longitude': -106.56739}, 'display_phone': '(505) 881-5293', 'distance': 3571.724649307866, 'id': 'fork-and-fig-albuquerque', 'image_url': 'https://s3-media1.fl.yelpcdn.com/bphoto/_-DpXKfS3jv6DyA47g6Fxg/o.jpg', 'is_closed': False, 'location': {'address1': '6904 Menaul Blvd NE',  'address2': 'Ste C',  'address3': '',  'city': 'Albuquerque',  'country': 'US',  'display_address': ['6904 Menaul Blvd NE', 'Ste C', 'Albuquerque, NM 87110'],  'state': 'NM',  'zip_code': '87110'}, 'name': 'Fork & Fig', 'phone': '+15058815293', 'price': '$$', 'rating': 4.5, 'review_count': 604}frontier_restaurant = {'categories': [{'alias': 'mexican', 'title': 'Mexican'},  {'alias': 'diners', 'title': 'Diners'},  {'alias': 'tradamerican', 'title': 'American (Traditional)'}], 'coordinates': {'latitude': 35.0808088832532, 'longitude': -106.619402244687}, 'display_phone': '(505) 266-0550', 'distance': 4033.6583235266075, 'id': 'frontier-restaurant-albuquerque-2', 'image_url': 'https://s3-media4.fl.yelpcdn.com/bphoto/M9L2z6-G0NobuDJ6YTh6VA/o.jpg', 'is_closed': True, 'location': {'address1': '2400 Central Ave SE',  'address2': '',  'address3': '',  'city': 'Albuquerque',  'country': 'US',  'display_address': ['2400 Central Ave SE', 'Albuquerque, NM 87106'],  'state': 'NM',  'zip_code': '87106'}, 'name': 'Frontier Restaurant', 'phone': '+15052660550', 'price': '$', 'rating': 4.0, 'review_count': 1369}restaurants = [fork_fig, frontier_restaurant]  def open_restaurants(restaurants):    selected = []    for i in restaurants:        if 'is_closed' in i:            if not i['is_closed']:                selected.append(i['name'])    return selectedprint(open_restaurants(restaurants))输出:['Fork & Fig']较短版本:使用列表理解:def open_restaurants(restaurants):    return [x['name'] for x in restaurants if 'is_closed' in x and not x["is_closed"]]print(open_restaurants(restaurants))使用get()而不是索引:def open_restaurants(restaurants):    return [x['name'] for x in restaurants if 'name' in x and not x.get('is_closed', True)]print(open_restaurants(restaurants))

胡子哥哥

在这里使用列表理解。它只有一行,易于阅读open_restaurants = [restaurant.get("name") for restaurant in restaurants if not restaurant.get("is_closed")]Output: ["Fork & Fig"]

有只小跳蛙

使用filter函数:def open_restaurants(restaurants):     return filter(lambda r: not r['is_closed'], restaurants)
随时随地看视频慕课网APP

相关分类

Python
我要回答