te**********@futurepundit.com 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 <mw*@mired.org>
http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.