在由字典构成的列表里查找 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
ThunderEX
V2EX    Python

在由字典构成的列表里查找

  •  
  •   ThunderEX 2012-09-24 15:19:58 +08:00 3544 次点击
    这是一个创建于 4815 天前的主题,其中的信息可能已经有所发展或是发生改变。
    listA = [{'ID'=14, ...},
    {'ID'=15, ...},
    ...,
    ]
    写一个简单好看的表达式,如果listA里面有'ID'==14的字典,就返回True?
    10 条回复    1970-01-01 08:00:00 +08:00
    skyleft
        1
    skyleft  
       2012-09-24 15:25:46 +08:00   1
    y=lambda listA:bool([x for x in listA if x['ID']==14])
    reus
        2
    reus  
       2012-09-24 15:26:22 +08:00
    any(map(lambda d: d.get('ID', None) == 14, l))
    013231
        3
    013231  
       2012-09-24 15:27:38 +08:00   2
    any(item['id'] == 14 for item in listA)
    reus
        4
    reus  
       2012-09-24 15:35:48 +08:00   1
    这个比刚才的更快,因为是generator,遇到为True的就直接返回了,不用遍历整个list
    any(d for d in l if d.get('ID', None) == 14)
    比如l = [{'ID': 14}] * 5000000,用这个会秒回,map的话慢很多= =
    ThunderEX
        5
    ThunderEX  
    OP
       2012-09-24 15:37:44 +08:00
    @reus 谢~我们用的是2.4.3没有any()……跪了……
    skyleft
        6
    skyleft  
       2012-09-24 15:47:57 +08:00
    @reus 赞generator
    013231
        7
    013231  
       2012-09-24 15:50:13 +08:00
    @ThunderEX 没有自己一就是了.
    http://gist.github.com/3774827
    ThunderEX
        8
    ThunderEX  
    OP
       2012-09-24 15:53:44 +08:00
    @013231 就是临时用一次,所以问问有没有一个表达式就OK的写得好看,没有的话就老老实实写for好了……
    Veelian
        9
    Veelian  
       2012-09-24 16:28:22 +08:00
    你确定list里是字典?
    ThunderEX
        10
    ThunderEX  
    OP
       2012-09-24 16:32:25 +08:00
    @Veelian 我已经发现自己把冒号写成等号了……
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2263 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 21ms UTC 15:49 PVG 23:49 LAX 07:49 JFK 10:49
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86