用python检测文件是否正在被修改

将[{},{}]转为dict

妙音 posted @ 2013年11月14日 21:31 in python , 2941 阅读

经常遇到一种需求,需要把从数据库取出的数据,转为dict对象([{}, {},...]-->dict)。

rs = [{"user_id":111, "name":"abc"}, {"user_id":123, "name":"edf"}]
print dict(map(lambda r:[r["user_id"], r], rs))
>>{111: {'user_id': 111, 'name': 'abc'}, 123: {'user_id': 123, 'name': 'edf'}}

上面看起来比较啰嗦,换一种写法

print dict([r["user_id"], r] for r in rs)

更简洁的写法

{r['user_id'] : r for r in rs}

 

 

Avatar_small
依云 说:
2013年11月14日 21:59

{r['user_id']: r for r in rs}

好像是 Python 2.7+ 支持。

Avatar_small
妙音 说:
2013年11月18日 09:28

@依云: 谢谢,这个特性很好用

Avatar_small
먹튀검증업체 说:
2020年12月27日 17:01

Great knowledge, do anyone mind merely reference back to it 먹튀검증업체

Avatar_small
betta fish 说:
2021年1月01日 00:19

definately enjoy every little bit of it and I have you bookmarked to check out new stuff of your blog a must read blog! betta fish

Avatar_small
How to screenshot on 说:
2021年1月04日 18:53

If you own an iPhone and running iOS 14 or later, you can now take a screenshot just by tapping its back thanks to a new accessibility feature called Back Tap, which you’ll have to configure for the task, and there are a few ways to take screenshot on iPhone different versions, and it will be easy to understand and take a screenshot on iPhone. How to screenshot on iPhone 11 pro

Avatar_small
BSNL 4G 说:
2021年1月04日 19:05

BSNL has come out with a new set of 4G data recharge plans for its customers with new launches from time to time as per the market requirements,BSNL 4G Check the list of bundled 4G data offers to increase the data consumption in each 4G enabled recharge plans listed provides with high speed unlimited data, Lets explore from our prepaid 4G mobile internet packs & recharge with a suitable mobile data pack providing double data offer and work from home data packs.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter