Re: List index method for complex list item types?



techiepundit@xxxxxxxxxxxxxxxx writes:
> I have a list of sockets that I use for select.select calls like this:
[...]
> But a thought struck me while writing this: Does Python not provide a
> way to search a list of sublists to find something on, say, the value
> of the first sublist item field as a way to find the index to the item
> one wants in the parent list?

No, it doesn't. It's not hard to write, though. ISTR something about
list methods growing optional arguments that might be usable for this
in 2.5, but you'd have to check it yourself. But a list is the wrong
tool to use for this problem.

> There does not appear to be an alternative to lists that has better
> functionality for this purpose. Dictionaries are immutable, right? One
> can't use dictionaries for doing look-ups on dynamically changing
> lists?

Dictionaries are mutable. But you could still use them this way.

> For efficiency's sake it seems to me one wants a search function on
> lists that returns two things:
> - index where the item found.
> - full item which matches on the particular field one is looking up
> on.

Indexing into a list is fast, so there's no really not much need for
this.

> Am I wrong in thinking Python doesn't really provide an automated way
> to search lists of complex things?

No, you're not wrong. But using lists is the wrong way to solve your
problem.

> Also, ii C++ one can use STL iterators to move thru a list or deque.
> But if one needs to advance thru a list with array indexes that does
> Python index in each time if one is looping thru the list?
>
> In Python maybe the trick is to use
> ii = 0
> For item in List
> # see if item matches
>
> ii = ii + 1
>
> and then somehow pop out of the for loop once one finds a match?

Use "break" to pop out of the list. Use "enumerate" to iterate through
both the list and an index into the list.

However, for your problem - finding data associated with a socket that
you're using in a select - a list is the wrong tool. Sockets are
usable as dictionary keys. So you can store arbitrary extra data in a
dictionary indexed by the sockets. That will be faster than searching
a list, even if the comparison is very simple.

Alternatively, subclass socket, add the data you want associated with
each socket to the instances of the subclass, and pass select
instances of your subclass.

<mike
--
Mike Meyer <mwm@xxxxxxxxx> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
.



Relevant Pages

  • Re: Why are tuples immutable?
    ... but yet the fact that lists are mutable and tuples ... as dictionaries are one example. ... in the second we just allow mutables to ... > Except that Python uses dicts internally, ...
    (comp.lang.python)
  • No typical loops, and other crazy ideas
    ... manipulation capabilities of Python (training was largely the ... I've tried to manipulate the data just in Python ... types of data structures (dictionaries, sets, lists) and arguably 6 ... I haven't figured out the way of producing a list of dictionaries ...
    (comp.lang.python)
  • Re: Lists implemented as integer-hashed Dictionaries?
    ... Python lists are just dictionaries with lists hashed by integers. ... I looked at the source code for lists in python, ...
    (comp.lang.python)
  • PyCheck for a classes defined in python and user data in PyObject_HEAD
    ... in C I want to check if a given PyObject is a xml.dom.minidom.Node (or ... I'm in a situation when i don't really need to extend python with any ... The reason i'm asking is because while for dictionaries i can stick ... object in a key with an obscure name while in lists this would change ...
    (comp.lang.python)
  • Re: Non-web-based templating system
    ... I'm creating a small application in Python that uses lists and ... dictionaries to create a rudimentary database. ...
    (comp.lang.python)