【问题标题】:filter out item from ordered dict从有序字典中过滤掉项目
【发布时间】:2018-06-29 15:28:53
【问题描述】:

对于以下有序字典,我怎样才能只打印 1)“价格”及其价值 2) 以对应的 'room_id' 降序排列

[OrderedDict([('room_id', '1133718'), ('survey_id', '1280'), ('host_id', '6219420'), ('room_type', 'Shared room'), ('country', ''), ('city', 'Singapore'), ('borough', ''), ('neighborhood', 'MK03'), ('reviews', '9'), ('overall_satisfaction', '4.5'), ('accommodates', '12'), ('bedrooms', '1.0'), ('bathrooms', ''), ('price', '74.0'), ('minstay', ''), ('last_modified', '2017-05-17 09:10:25.431659'), ('latitude', '1.293354'), ('longitude', '103.769226'), ('location', '0101000020E6100000E84EB0FF3AF159409C69C2F693B1F43F')]), OrderedDict([('room_id', '3179080'), ('survey_id', '1280'), ('host_id', '15295886'), ('room_type', 'Shared room'), ('country', ''), ('city', 'Singapore'), ('borough', ''), ('neighborhood', 'TS17'), ('reviews', '15'), ('overall_satisfaction', '5.0'), ('accommodates', '12'), ('bedrooms', '1.0'), ('bathrooms', ''), ('price', '77.0'), ('minstay', ''), ('last_modified', '2017-05-17 09:10:24.216548'), ('latitude', '1.310862'), ('longitude', '103.858828'), ('location', '0101000020E6100000E738B709F7F659403F1BB96E4AF9F43F')]), OrderedDict([('room_id', '15303457'), ('survey_id', '1280'), ('host_id', '97053568'), ('room_type', 'Shared room'), ('country', ''), ('city', 'Singapore'), ('borough', ''), ('neighborhood', 'MK05'), ('reviews', '0'), ('overall_satisfaction', '0.0'), ('accommodates', '14'), ('bedrooms', '1.0'), ('bathrooms', ''), ('price', '60.0'), ('minstay', ''), ('last_modified', '2017-05-17 09:10:16.969900'), ('latitude', '1.333744'), ('longitude', '103.764612'), ('location', '0101000020E610000044882B67EFF0594093C7D3F20357F53F')])]

【问题讨论】:

    标签: python python-3.x dictionary ordereddictionary


    【解决方案1】:

    给定一个 OrderedDicts 列表:

    from collections import OrderedDict
    li=[OrderedDict([('room_id', '1133718'), ('survey_id', '1280'), ('host_id', '6219420'), ('room_type', 'Shared room'), ('country', ''), ('city', 'Singapore'), ('borough', ''), ('neighborhood', 'MK03'), ('reviews', '9'), ('overall_satisfaction', '4.5'), ('accommodates', '12'), ('bedrooms', '1.0'), ('bathrooms', ''), ('price', '74.0'), ('minstay', ''), ('last_modified', '2017-05-17 09:10:25.431659'), ('latitude', '1.293354'), ('longitude', '103.769226'), ('location', '0101000020E6100000E84EB0FF3AF159409C69C2F693B1F43F')]), OrderedDict([('room_id', '3179080'), ('survey_id', '1280'), ('host_id', '15295886'), ('room_type', 'Shared room'), ('country', ''), ('city', 'Singapore'), ('borough', ''), ('neighborhood', 'TS17'), ('reviews', '15'), ('overall_satisfaction', '5.0'), ('accommodates', '12'), ('bedrooms', '1.0'), ('bathrooms', ''), ('price', '77.0'), ('minstay', ''), ('last_modified', '2017-05-17 09:10:24.216548'), ('latitude', '1.310862'), ('longitude', '103.858828'), ('location', '0101000020E6100000E738B709F7F659403F1BB96E4AF9F43F')]), OrderedDict([('room_id', '15303457'), ('survey_id', '1280'), ('host_id', '97053568'), ('room_type', 'Shared room'), ('country', ''), ('city', 'Singapore'), ('borough', ''), ('neighborhood', 'MK05'), ('reviews', '0'), ('overall_satisfaction', '0.0'), ('accommodates', '14'), ('bedrooms', '1.0'), ('bathrooms', ''), ('price', '60.0'), ('minstay', ''), ('last_modified', '2017-05-17 09:10:16.969900'), ('latitude', '1.333744'), ('longitude', '103.764612'), ('location', '0101000020E610000044882B67EFF0594093C7D3F20357F53F')])]
    

    你可以这样做:

    >>> li2=[OrderedDict([('room_id',od['room_id']),('price',od['price'])]) for od in li]
    >>> sorted(li2, key=lambda od: float(od['price']), reverse=True)
    [OrderedDict([('room_id', '3179080'), ('price', '77.0')]), OrderedDict([('room_id', '1133718'), ('price', '74.0')]), OrderedDict([('room_id', '15303457'), ('price', '60.0')])]
    

    如果你想格式化打印:

    >>> li3=sorted(li2, key=lambda od: float(od['price']), reverse=True)
    >>> print("\n".join(["Room ID: {} Price: {}".format(od['room_id'], od['price']) for od in li3]))
    Room ID: 3179080 Price: 77.0
    Room ID: 1133718 Price: 74.0
    Room ID: 15303457 Price: 60.0
    

    【讨论】:

    • 非常感谢,您保存了我的项目 :)
    • @ArjunRajendran 如果这个答案解决了您的问题,请consider accepting it.
    【解决方案2】:

    @Arjun,你也可以试试下面的代码来解决你的问题。

    该解决方案使用 list comprehension 和 sorted() 函数及其关键字参数之一 key 的概念。

    from collections import OrderedDict
    
    dicts = [
        OrderedDict([
            ('room_id', '1133718'), ('survey_id', '1280'),
            ('host_id', '6219420'), ('room_type', 'Shared room'), 
            ('country', ''), ('city', 'Singapore'), ('borough', ''), 
            ('neighborhood', 'MK03'), ('reviews', '9'), 
            ('overall_satisfaction', '4.5'), ('accommodates', '12'), 
            ('bedrooms', '1.0'), ('bathrooms', ''), ('price', '74.0'), 
            ('minstay', ''), ('last_modified', '2017-05-17 09:10:25.431659'), 
            ('latitude', '1.293354'), ('longitude', '103.769226'), 
            ('location', '0101000020E6100000E84EB0FF3AF159409C69C2F693B1F43F')]), 
        OrderedDict([('room_id', '3179080'), ('survey_id', '1280'), 
            ('host_id', '15295886'), ('room_type', 'Shared room'), 
            ('country', ''), ('city', 'Singapore'), ('borough', ''), 
            ('neighborhood', 'TS17'), ('reviews', '15'), 
            ('overall_satisfaction', '5.0'), ('accommodates', '12'), 
            ('bedrooms', '1.0'), ('bathrooms', ''), ('price', '77.0'), 
            ('minstay', ''), ('last_modified', '2017-05-17 09:10:24.216548'), 
            ('latitude', '1.310862'), ('longitude', '103.858828'), 
            ('location', '0101000020E6100000E738B709F7F659403F1BB96E4AF9F43F')]), 
        OrderedDict([('room_id', '15303457'), ('survey_id', '1280'), 
            ('host_id', '97053568'), ('room_type', 'Shared room'), 
            ('country', ''), ('city', 'Singapore'), ('borough', ''), 
            ('neighborhood', 'MK05'), ('reviews', '0'), 
            ('overall_satisfaction', '0.0'), ('accommodates', '14'), 
            ('bedrooms', '1.0'), ('bathrooms', ''), ('price', '60.0'), 
            ('minstay', ''), ('last_modified', '2017-05-17 09:10:16.969900'), 
            ('latitude', '1.333744'), ('longitude', '103.764612'), 
            ('location', '0101000020E610000044882B67EFF0594093C7D3F20357F53F')])];
    
    output = '\n'.join([ 'room_id: {}, price: {}'.format(
        item['room_id'], item['price']
    ) for item in sorted(dicts, key=lambda d: int(d['room_id']))])
    
    print(output);
    

    输出 »

    room_id: 1133718, price: 74.0
    room_id: 3179080, price: 77.0
    room_id: 15303457, price: 60.0
    

    【讨论】:

    • 谢谢!这很有帮助! :)
    • 没问题@Arjun。感谢您的宝贵回复。
    【解决方案3】:

    要通过price 订购,您可以使用sorted 和自定义键:

    res = sorted(L, key=lambda x: float(x['price']), reverse=True)
    

    要将结果提取为 price 和 room_id 的组合,您可以使用列表推导:

    res_id_price = [(x['room_id'], x['price']) for x in res]
    

    如果您想打印并且不需要列表:

    print(*((x['room_id'], x['price']) for x in res), sep='\n')
    
    ('3179080', '77.0')
    ('1133718', '74.0')
    ('15303457', '60.0')
    

    【讨论】:

      猜你喜欢
      • 2020-10-12
      • 1970-01-01
      • 1970-01-01
      • 2011-01-09
      • 1970-01-01
      • 2021-12-11
      • 2022-11-02
      • 1970-01-01
      • 2021-11-25
      相关资源
      最近更新 更多