By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,977 Members | 1,352 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,977 IT Pros & Developers. It's quick & easy.

List index method for complex list item types?

P: n/a
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?

Dec 31 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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.
Dec 31 '05 #2

P: n/a
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([b]) 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 (

Dec 31 '05 #3

P: n/a
te**********@futurepundit.com writes:
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([b]) 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
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Dec 31 '05 #4

P: n/a
On Fri, 30 Dec 2005 18:56:47 -0800, techiepundit wrote:
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?

--
Steven.

Dec 31 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.