0
JSON結構和匹配值解析名單我有以下的代碼,給出了基於IP的城市:與蟒蛇
RANGES = {
'Paris': [
{'start': '10.10.0.0', 'end': '10.10.255.255'},
{'start': '192.168.1.0', 'end': '192.168.1.255'},
],
'NYC': [
{'start': '10.12.0.0', 'end': '10.12.255.255'},
{'start': '172.16.10.0', 'end': '172.16.11.255'},
{'start': '192.168.2.0', 'end': '192.168.2.255'},
]
}
def get_city(self, ip):
print 'Here'
for city, ipranges in self.RANGES.items():
for iprange in ipranges:
if ip >= iprange['start'] and ip <= iprange['end']:
return city
我有另一個變量:
RECORDS = [
{'user_id': 1, 'created_at': '2017-01-01T10:00:00', 'status': 'paying'},
{'user_id': 1, 'created_at': '2017-03-01T19:00:00', 'status': 'paying'},
{'user_id': 1, 'created_at': '2017-02-01T12:00:00', 'status': 'cancelled'},
{'user_id': 3, 'created_at': '2017-10-01T10:00:00', 'status': 'paying'},
{'user_id': 3, 'created_at': '2016-02-01T05:00:00', 'status': 'cancelled'},
]
對於那些我試圖實現此功能:
def get_status(self, user_id, date): //should return the status
我不知道如何解析列表 - >匹配值 - >返回狀態對於created_at
和user_id