Re: [Tutor] question about a list of lists

2007-01-29 Thread Kent Johnson
shawn bright wrote:
> Thanks Kent,
>i am going with option A, the helper set, because i also need to 
> count the occurances and this seems to be the easiest solution.

If you need the number of unique items that is just the length of the 
final list. If you need the number of occurrences of each initial item 
then a dict mapping initial item to count (or maybe a list of lists) 
would be the way to go.

Kent

> 
> thanks for your help.
> 
> shawn
> 
> On 1/28/07, *Kent Johnson* <[EMAIL PROTECTED] > 
> wrote:
> 
> shawn bright wrote:
>  > lo there all.
>  >
>  > i have a list of lists that i want to build, only if an item is
> not in
>  > the list already.
>  >
>  > kinda like this
>  > new_list = []
>  > for item in lists: # item will look something like [var1, var2,
> var3]
>  > if item[0] in new_list ( only the first element of each list
> ) like
>  > new_list[0][0]
>  >
>  > basicly, i want to know if item[0]   is one of the items[0] in my
> new_list
>  >
>  > whats a good pythonic way to do this? i mean, i have a couple of
> way to
>  > do this, but they are ugly.
> 
> One way to do this is to keep a helper set that contains the first
> elements of each list. Something like
> new_list = []
> firsts = set()
> for item in lists:
>if item[0] not in firsts:
>  new_list.append(item)
>  firsts.add(item[0]
> 
> If you don't care about the order of the result, and if two lists have
> duplicate first items you are happy to use the first, then you could use
> a dict mapping first item to list:
> 
> new_list = dict((item[0], item) for item in lists).values()
> 
> Kent
> 
> 


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] question about a list of lists

2007-01-29 Thread shawn bright

Thanks Kent,
  i am going with option A, the helper set, because i also need to count
the occurances and this seems to be the easiest solution.

thanks for your help.

shawn

On 1/28/07, Kent Johnson <[EMAIL PROTECTED]> wrote:


shawn bright wrote:
> lo there all.
>
> i have a list of lists that i want to build, only if an item is not in
> the list already.
>
> kinda like this
> new_list = []
> for item in lists: # item will look something like [var1, var2, var3]
> if item[0] in new_list ( only the first element of each list ) like
> new_list[0][0]
>
> basicly, i want to know if item[0]   is one of the items[0] in my
new_list
>
> whats a good pythonic way to do this? i mean, i have a couple of way to
> do this, but they are ugly.

One way to do this is to keep a helper set that contains the first
elements of each list. Something like
new_list = []
firsts = set()
for item in lists:
   if item[0] not in firsts:
 new_list.append(item)
 firsts.add(item[0]

If you don't care about the order of the result, and if two lists have
duplicate first items you are happy to use the first, then you could use
a dict mapping first item to list:

new_list = dict((item[0], item) for item in lists).values()

Kent


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] question about a list of lists

2007-01-28 Thread Kent Johnson
shawn bright wrote:
> lo there all.
> 
> i have a list of lists that i want to build, only if an item is not in 
> the list already.
> 
> kinda like this
> new_list = []
> for item in lists: # item will look something like [var1, var2, var3]
> if item[0] in new_list ( only the first element of each list ) like 
> new_list[0][0]
> 
> basicly, i want to know if item[0]   is one of the items[0] in my new_list
> 
> whats a good pythonic way to do this? i mean, i have a couple of way to 
> do this, but they are ugly.

One way to do this is to keep a helper set that contains the first 
elements of each list. Something like
new_list = []
firsts = set()
for item in lists:
   if item[0] not in firsts:
 new_list.append(item)
 firsts.add(item[0]

If you don't care about the order of the result, and if two lists have 
duplicate first items you are happy to use the first, then you could use 
a dict mapping first item to list:

new_list = dict((item[0], item) for item in lists).values()

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] question about a list of lists

2007-01-28 Thread Alan Gauld

"shawn bright" <[EMAIL PROTECTED]> wrote

> new_list = []
> for item in lists: # item will look something like [var1, var2, 
> var3]
>if item[0] in new_list

> basicly, i want to know if item[0]   is one of the items[0] in my 
> new_list

Your pseudo code is pretty much exactly right.
What more are you looking for?

Alan G. 


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] question about a list of lists

2007-01-28 Thread shawn bright

lo there all.

i have a list of lists that i want to build, only if an item is not in the
list already.

kinda like this
new_list = []
for item in lists: # item will look something like [var1, var2, var3]
   if item[0] in new_list ( only the first element of each list ) like
new_list[0][0]

basicly, i want to know if item[0]   is one of the items[0] in my new_list

whats a good pythonic way to do this? i mean, i have a couple of way to do
this, but they are ugly.

shawn
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor