亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我如何編寫一個函數,將餐廳列表作為參數并返回僅包含未關閉餐廳的列表?

我如何編寫一個函數,將餐廳列表作為參數并返回僅包含未關閉餐廳的列表?

千萬里不及你 2022-01-05 19:42:38
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下面是我一直在處理的代碼,我不太確定如何解決這個問題以獲得我想要返回的值。
查看完整描述

3 回答

?
至尊寶的傳說

TA貢獻1789條經驗 獲得超10個贊

繼續評論:


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


查看完整回答
反對 回復 2022-01-05
?
胡子哥哥

TA貢獻1825條經驗 獲得超6個贊

在這里使用列表理解。它只有一行,易于閱讀


open_restaurants = [restaurant.get("name") for restaurant in restaurants if not restaurant.get("is_closed")]


Output: ["Fork & Fig"]


查看完整回答
反對 回復 2022-01-05
?
有只小跳蛙

TA貢獻1824條經驗 獲得超8個贊

使用filter函數:

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


查看完整回答
反對 回復 2022-01-05
  • 3 回答
  • 0 關注
  • 156 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號