亿万教师在线注册平台python 字典构成的列表怎样搜刮 ?

如下列表,由字典构成,怎样高效找到 uNickName: u小蜻蜓的这个字典东西?一个一个遍历固然可以实行,不过总以为不太高效.数据量大的话,有什么高效的好办法?或许python有什么原生的办法?

[
    {
        uUserName: u@27ed44102dd86ec42eb1a572b283b5b8,
        uRemarkPYQuanPin: u,
        uDisplayName: u,
        uKeyWord: u,
        uPYInitial: u,
        uUin: 112266535,
        uRemarkPYInitial: u,
        uPYQuanPin: u,
        uMemberStatus: 0,
        uNickName: u一二三,
        uAttrStatus: 2181050407L
    },
    {
        uUserName: u@7a9e7af3a886a6c7d7219f0e017e5b586263de82e32300c267a78202118e7573,
        uRemarkPYQuanPin: u,
        uDisplayName: u,
        uKeyWord: u,
        uPYInitial: u,
        uUin: 22720082935L,
        uRemarkPYInitial: u,
        uPYQuanPin: u,
        uMemberStatus: 0,
        uNickName: u每天,
        uAttrStatus: 102469
    },
    {
        uUserName: u@886fe820eacd4899505b86469257f56be7f95e8a433109f85e23e12c27a575f6,
        uRemarkPYQuanPin: u,
        uDisplayName: u,
        uKeyWord: u,
        uPYInitial: u,
        uUin: 33135252892L,
        uRemarkPYInitial: u,
        uPYQuanPin: u,
        uMemberStatus: 0,
        uNickName: u李金芬,
        uAttrStatus: 4165
    },
    {
        uUserName: u@c5c563df9c089b12eeca1e68dd6745ebf8963092791efd3a17b0867ade949944,
        uRemarkPYQuanPin: u,
        uDisplayName: u,
        uKeyWord: u,
        uPYInitial: u,
        uUin: 402283062,
        uRemarkPYInitial: u,
        uPYQuanPin: u,
        uMemberStatus: 0,
        uNickName: u小蜻蜓,
        uAttrStatus: 4133
    }
]

在你数据构造不该变的情况下,仍然只能是遍历,只是Python可以写的很优雅,不用去写for循环:

# -*- coding: UTF-8 -*
users = [
    {
        uUserName: u@27ed44102dd86ec42eb1a572b283b5b8,
        uRemarkPYQuanPin: u,
        uDisplayName: u,
        uKeyWord: u,
        uPYInitial: u,
        uUin: 112266535,
        uRemarkPYInitial: u,
        uPYQuanPin: u,
        uMemberStatus: 0,
        uNickName: u一二三,
        uAttrStatus: 2181050407L
    },
    {
        uUserName: u@7a9e7af3a886a6c7d7219f0e017e5b586263de82e32300c267a78202118e7573,
        uRemarkPYQuanPin: u,
        uDisplayName: u,
        uKeyWord: u,
        uPYInitial: u,
        uUin: 22720082935L,
        uRemarkPYInitial: u,
        uPYQuanPin: u,
        uMemberStatus: 0,
        uNickName: u每天,
        uAttrStatus: 102469
    },
    {
        uUserName: u@886fe820eacd4899505b86469257f56be7f95e8a433109f85e23e12c27a575f6,
        uRemarkPYQuanPin: u,
        uDisplayName: u,
        uKeyWord: u,
        uPYInitial: u,
        uUin: 33135252892L,
        uRemarkPYInitial: u,
        uPYQuanPin: u,
        uMemberStatus: 0,
        uNickName: u李金芬,
        uAttrStatus: 4165
    },
    {
        uUserName: u@c5c563df9c089b12eeca1e68dd6745ebf8963092791efd3a17b0867ade949944,
        uRemarkPYQuanPin: u,
        uDisplayName: u,
        uKeyWord: u,
        uPYInitial: u,
        uUin: 402283062,
        uRemarkPYInitial: u,
        uPYQuanPin: u,
        uMemberStatus: 0,
        uNickName: u小蜻蜓,
        uAttrStatus: 4133
    }
]

print filterlambda x:x[uNickName] == u小蜻蜓, users

假如数据构造可以变的话,你可以做一个字典完成NickName到用户的映射,查找的时分直接经过NickName来获取用户(条件是NickName不克不及反复)。

实际消费进程中,看看有无可以把数据存到MongoDB里面去,对NickName做索引,速率也能下去了。

这种情况很适适用 nosql吧,mongodb或许elasticsearch,

(看完/读完)这篇文章有何感想! 亿万先生在线注册平台的分享…

发表评论

姓名 *
电子邮件 *
站点