List index method for complex list item types?

T

techiepundit

I'm a Python newbie who just started learning the language a few weeks
ago. So these are beginner questions.

I have a list of sockets that I use for select.select calls like this:

ReadList,WriteList,EventList = select.select(self.SocketList,[],[],3)

In parallel with that list of sockets I want something that is like a
list of socket,PacketFragment pairs. I need this because I could do
recv and get part of a sent packet. I want to loop around and each time
add more to the fragment until I get a full packet which has an
internal byte structure of the types I'm expecting.

The idea is every time I do
self.SocketList.append(NewSocket)
also do
self.SocketPacketFragmentList.append((NewSocket,''))
so that the index into SocketList is the same as the index into
SocketPacketFragmentList. So I can use
self.SocketList.index(SomeSocket) find an item in SocketList and then
know how to find it in the other list.

MySocketIndex = self.SocketList.index(MyTargetSocket)

Then do:

MySubList = self.SocketPacketFragmentList[MySocketIndex]

Then

MyPacketFragment = MySubList[1]

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?

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?

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.

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

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?
 
M

Mike Meyer

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
 
T

techiepundit

Mike,

I'm trying to figure out dictionaries using the documentation. Clicking
on "dictionary type" takes me to "2.3.8 Mapping Types -- classdict". Is
that the documentation for the dictionary type? If so, I do not see an
"append" or "add" or "insert" method defined in the list of methods on
that page.

Here's what they list:

Operation Result Notes
len(a) the number of items in a
a[k] the item of a with key k (1)
a[k] = v set a[k] to v
del a[k] remove a[k] from a (1)
a.clear() remove all items from a
a.copy() a (shallow) copy of a
a.has_key(k) True if a has a key k, else False
k in a Equivalent to a.has_key(k) (2)
k not in a Equivalent to not a.has_key(k) (2)
a.items() a copy of a's list of (key, value) pairs (3)
a.keys() a copy of a's list of keys (3)
a.update() updates (and overwrites) key/value pairs from b (9)
a.fromkeys(seq[, value]) Creates a new dictionary with keys from seq
and values set to value (7)
a.values() a copy of a's list of values (3)
a.get(k[, x]) a[k] if k in a, else x (4)
a.setdefault(k[, x]) a[k] if k in a, else x (also setting it) (5)
a.pop(k[, x]) a[k] if k in a, else x (and remove k) (8)
a.popitem() remove and return an arbitrary (key, value) pair (6)
a.iteritems() return an iterator over (key, value) pairs (2), (3)
a.iterkeys() return an iterator over the mapping's keys (2), (3)
a.itervalues() return an iterator over the mapping's values (
 
S

Steven D'Aprano

Mike,

I'm trying to figure out dictionaries using the documentation. Clicking
on "dictionary type" takes me to "2.3.8 Mapping Types -- classdict". Is
that the documentation for the dictionary type? If so, I do not see an
"append" or "add" or "insert" method defined in the list of methods on
that page.

No you don't. Why do you think dicts need one?
Here's what they list:

Operation Result Notes
len(a) the number of items in a
a[k] the item of a with key k (1)
a[k] = v set a[k] to v

x = a[k] # get the value in dict a with key k
a[k] = x # set the value in dict a with key k

What more do you need?
 
M

Mike Meyer

I'm trying to figure out dictionaries using the documentation. Clicking
on "dictionary type" takes me to "2.3.8 Mapping Types -- classdict". Is
that the documentation for the dictionary type? If so, I do not see an
"append" or "add" or "insert" method defined in the list of methods on
that page.
Here's what they list:

Operation Result Notes
len(a) the number of items in a
a[k] the item of a with key k (1)
a[k] = v set a[k] to v
del a[k] remove a[k] from a (1)
a.clear() remove all items from a
a.copy() a (shallow) copy of a
a.has_key(k) True if a has a key k, else False
k in a Equivalent to a.has_key(k) (2)
k not in a Equivalent to not a.has_key(k) (2)
a.items() a copy of a's list of (key, value) pairs (3)
a.keys() a copy of a's list of keys (3)
a.update() updates (and overwrites) key/value pairs from b (9)
a.fromkeys(seq[, value]) Creates a new dictionary with keys from seq
and values set to value (7)
a.values() a copy of a's list of values (3)
a.get(k[, x]) a[k] if k in a, else x (4)
a.setdefault(k[, x]) a[k] if k in a, else x (also setting it) (5)
a.pop(k[, x]) a[k] if k in a, else x (and remove k) (8)
a.popitem() remove and return an arbitrary (key, value) pair (6)
a.iteritems() return an iterator over (key, value) pairs (2), (3)
a.iterkeys() return an iterator over the mapping's keys (2), (3)
a.itervalues() return an iterator over the mapping's values (


Yup, that's it. The 3rd item in the list is what you're (probably -
I'm practicing my mindreading here, since you didn't say what you were
trying to do) looking for.

<mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top