What's the best way to search for a Python dictionary value in a list of dictionaries?

前端 未结 4 2099
一整个雨季
一整个雨季 2020-12-14 09:20

I have the following data structure:

  data = [
      {\'site\': \'Stackoverflow\', \'id\': 1},
      {\'site\': \'Superuser\', \'id\': 2}, 
      {\'site\':         


        
4条回答
  •  情歌与酒
    2020-12-14 09:58

    I'm not sure of the python syntax, but it might work for you this way. While building your primary data structure, also build a parallel one that's a hash or associative array keyed on the site name; then to see if a given site exists you attempt a lookup in the hash with the site name. If it succeeds, you know there's a record in your data structure for that site and you've done it in the time of the hash lookup (likely O(1) or O(log2(n)) depending on the hash technique) instead of the O(n/2) of the list traversal.

    (updated while writing: this is pretty much what S.Lott posted)

提交回复
热议问题