我有以下结构的大量字典:
data1 =
[{'_id': '4nPkqDbLZU',
'data_Temperature': 14,
'data_time_utc': 1403560800,
'location_0': 9.6071829,
'data_Rain': 0,
'modules_0': '02:00:00:00:0b:60',
'modules_1': '05:00:00:01:3c:5c',
'location_1': 55.726223,
'altitude': 74},
{'_id': '4nPkqDbL855',
'data_Temperature': 29,
'data_time_utc': 1403560900,
'location_0': 9.6071829,
'data_Rain': 0,
'modules_0': '02:00:00:00:0b:60',
'modules_1': '05:00:00:01:3c:5c',
'location_1': 55.726223,
'altitude': 74},]
然后我创建了以下代码,仅选择所需的数据并同时重命名字典键:
d1 = []
for x in data1:
data_dict = {
"id": x.get('_id'),
"Temperature": x.get('data_Temperature'),
"time_utc": x.get('data_time_utc'),
"longitude": x.get('location_0'),
"latitude": x.get('location_1'),
"altitude": x.get('altitude'),
}
d1.append(data_dict)
它工作正常,但是我需要对许多其他字典列表执行相同的指令。所以我认为方法是将代码包装在一个函数中。我尝试了不同的方法,但没有成功。有什么建议吗?
答案 0 :(得分:3)
使用var express = require('express');
var app = express();
const parseArgs = require('minimist') (process.argv.slice(2))
const IP = parseArgs.ip || "127.0.0.1"
const PORT = parseArgs.port || 8000
app.get("/", function (req, res) {
res.send("Server running on " + IP + ":" + PORT)
})
console.log("START ", IP+" :",PORT)
app.listen(PORT, IP)
module.exports = app;
和lambda
的一种方法
演示:
map
输出:
makeDict = lambda x: {
"id": x.get('_id'),
"Temperature": x.get('data_Temperature'),
"time_utc": x.get('data_time_utc'),
"longitude": x.get('location_0'),
"latitude": x.get('location_1'),
"altitude": x.get('altitude'),
}
print( list(map(makeDict, data1)) )
答案 1 :(得分:1)
如果您想自由调用此类函数,可以如下进行分解:
def filter_dict(dict_list, **kwargs):
return [{key:dic.get(val) for key, val in kwargs.items() }
for dic in dict_list ]
该函数将dict列表作为第一个参数,然后将所有要用作关键字的键用作新名称:
print(filter_dict(data1,
id='_id',
Temperature='data_Temperature',
time_utc='data_time_utc' ))
>>>[{'id': '4nPkqDbLZU', 'Temperature': 14, 'time_utc': 1403560800},
{'id': '4nPkqDbL855', 'Temperature': 29, 'time_utc': 1403560900}]
答案 2 :(得分:0)
您可以编写一个使用列表理解功能返回列表的函数。这将比手动for
循环或通过map
的功能解决方案更有效。
def reformat_dict(d):
return [{"id": x.get('_id'),
"Temperature": x.get('data_Temperature'),
"time_utc": x.get('data_time_utc'),
"longitude": x.get('location_0'),
"latitude": x.get('location_1'),
"altitude": x.get('altitude')}
for item in d]
res = reformat_dict(data1)
print(res)
[{'Temperature': 29,
'altitude': 74,
'id': '4nPkqDbL855',
'latitude': 55.726223,
'longitude': 9.6071829,
'time_utc': 1403560900},
{'Temperature': 29,
'altitude': 74,
'id': '4nPkqDbL855',
'latitude': 55.726223,
'longitude': 9.6071829,
'time_utc': 1403560900}]
答案 3 :(得分:0)
mapping = dict(id='_id', Temperature='data_Temperature', time_utc='data_time_utc',
longitude='location_0', latitude='location_1', altitude='altitude')
nd = list(map(lambda d: {k: d.get(mapping[k], '') for k,v in mapping.items()}, data1))
pprint(nd)
输出:
[{'Temperature': 14,
'altitude': 74,
'id': '4nPkqDbLZU',
'latitude': 55.726223,
'longitude': 9.6071829,
'time_utc': 1403560800},
{'Temperature': 29,
'altitude': 74,
'id': '4nPkqDbL855',
'latitude': 55.726223,
'longitude': 9.6071829,
'time_utc': 1403560900}]