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

时间:2019-04-10 09:28:58

标签: python function conditional

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

下面是我一直在努力的代码,无法完全确定如何解决此问题以获取要返回的值。

def open_restaurants(restaurants):
    selected = []
    for i in restaurants:
        if fork_fig['is_closed']: 
            selected = restaurants[1]
        else:
            selected = restaurants[0]

    return selected

4 个答案:

答案 0 :(得分:1)

从评论继续:

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 selected

print(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))

答案 1 :(得分:0)

使用filter函数:

def open_restaurants(restaurants):
    return filter(lambda r: not r['is_closed'], restaurants)

答案 2 :(得分:0)

在此处使用列表理解。它的一行,易于阅读

.java

答案 3 :(得分:0)

有一种简单的方法可以将所有开放式餐厅添加到新列表中并打印其名称。 您将需要两个循环来完成此操作。

这是方法:

restaurants = [fork_fig, frontier_restaurant] # a list of restaurants
def open_restaurants(restaurants):
    selected = [] 
    for i in restaurants:  # append all open restaurants to a new list
        if not i['is_closed']:
            selected.append(i)
    return selected

my_open_restaurants = open_restaurants(restaurants) # call the function

for rest in my_open_restaurants:  # print all the names
    print(rest['name'])     

在您的示例中,输出将为:Fork & Fig