0

生成了如下列表:values = [{'uid':1},{'name','Jack'},{'uid':2}]
uid=1 和 uid=2 是用不同的SQL查出的相同字段json中解出的键值对,想问下如何重命名键uid分别为uid_1,uid_2 使得最终键具有唯一性,方便插入到数据库的uid_1,uid_2列中?

everfight 1.9k
2017-10-26 提问

1 个回答

3

已采纳
values = [{'uid':1},{'name','Jack'},{'uid':2}]

def foo(item):
    if isinstance(item,dict) and item.get('uid'):
        item['uid_' + str(item['uid'])] = item['uid']
        del item['uid']
    return item

# python 2
# bar = map(foo, values)

# python 3
bar = list(map(foo, values))

print(bar)

推广链接