Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > List index method for complex list item types?

Reply
Thread Tools

List index method for complex list item types?

 
 
techiepundit@futurepundit.com
Guest
Posts: n/a
 
      12-31-2005
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?

 
Reply With Quote
 
 
 
 
Mike Meyer
Guest
Posts: n/a
 
      12-31-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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 <(E-Mail Removed)> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
 
Reply With Quote
 
 
 
 
techiepundit@futurepundit.com
Guest
Posts: n/a
 
      12-31-2005
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) (
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 (

 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      12-31-2005
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.

 
Reply With Quote
 
Mike Meyer
Guest
Posts: n/a
 
      12-31-2005
(E-Mail Removed) 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) (
> 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 <(E-Mail Removed)> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sorting index-15, index-9, index-110 "the human way"? Tomasz Chmielewski Perl Misc 4 03-04-2008 05:01 PM
wsdl2java: method parameter a complex type that extends another complex type Robert Mark Bram Java 0 02-04-2007 10:06 AM
List Util insert_at(index, value), delete_at(index) ngoc Perl Misc 5 05-11-2006 12:31 PM
sorting list and then return the index of the sorted item custard_pie Python 10 05-03-2005 07:41 PM
Find index of item in list Sean Berry Python 2 12-07-2004 08:07 PM



Advertisments