Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ordered_dict help
#1
Hi guys,

I'm super new to coding and practically clueless. One of my homework requires me to work with large data files and we have to convert them to list of dictionaries which I have.
Afterwards, we are to sort the data according to

1. What are the top 10 priciest rooms in Singapore? List the room_id as well as price
2. Which hosts in Singapore have more than 30 listings. How many of such hosts do we have?
3. What are the different room types, and how many rooms of each room types are there in Singapore?
4. What is the average price of the rooms in the neighbourhood MK03?
5. For hosts that host more than 10 listings, which host_id has the highest average satisfaction level?

This is my first time doing ordereddict and I'm stuck on how to sort them.
Sample data [0:3]
[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')])]

Hope someone can help me out. Huh
Reply
#2
Some hints,and use Code tags.
Also mention that dictionary are ordered guarantee if use Python 3.7.
So now at top get highest price room and can take out eg room_id.
>>> s = sorted(rooms, key=lambda item: item['price'], reverse=True)
>>> s[0]['price']
'77.0'
>>> s[0]['room_id']
'3179080'
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020