two little syntax questions

Roman Suzi rnd at onego.ru
Thu Aug 30 09:25:46 EDT 2001


On Thu, 30 Aug 2001, Marcus Stojek wrote:

> Hi,
> 
> Two little syntax questions:
> 1.-----------------------------
> I have a huge dict of 3-tuples. I need the min and max value
> of all tuple[0] entries.

min0 = min(map(lambda x: x[0], dict.values()))
max0 = max(map(lambda x: x[0], dict.values()))
 
> I would like to use reduce. Something like:
> 
> nodes[1]=(3,2,5)
> nodes[2]=(-1,2,9)
> nodes[3]=(0,1,6)
> nodes[4]=(2,9,1)
> 
> minx = reduce(lambda a,b:min(a,b),nodes???and now what)

Or in one go:

mn, mx = reduce(
  lambda o,t: (min(o[0], t[0]), max(o[1], t[0])),
  nodes.values(), (nodes[1][0], nodes[1][0]))

vals = nodes.values()
mn, mx = reduce(
    lambda o,t: (min(o[0], t[0]), max(o[1], t[0])),  # new min, max
    vals,                        # tuples
    (vals[0][0], vals[0][0])     # initial min, max
  )


Instead of vals[0][0] and vals[0][0] some absolute min and max
values could be used.
 
> how do I explain: take first entry in tuple for all keyvalues
> 2.------------------------------
> 
> I have to print the remaining entries of a list
> (first entries are printed in groups of 8 per line)
> 
> lines = len(list)/8
> rest = len(list)%8
> print rest * "i," % (list[lines*8:])

What about:

lst =[1]*75
rest = lst[-(len(lst)%8):]
print string.join(map(str,rest), ",")

1,1,1

Also, rest could be printed in main loop as well.
There is no need to put separately:

>>> lst = [1]*75
>>> for p in range(0, len(lst), 8):
...   print string.join(map(str,lst[p:p+8]), ",")
...
1,1,1,1,1,1,1,1
1,1,1,1,1,1,1,1
1,1,1,1,1,1,1,1
1,1,1,1,1,1,1,1
1,1,1,1,1,1,1,1
1,1,1,1,1,1,1,1
1,1,1,1,1,1,1,1
1,1,1,1,1,1,1,1
1,1,1,1,1,1,1,1
1,1,1
>>>


 
> doesn't work. Okay I know print is expecting a tuple and
> list[lines*8:] is not. So how can I transform this into
> a tuple.
> 
> Thanks a lot
> Marcus

Nice questions!

Sincerely yours, Roman A.Suzi
-- 
 - Petrozavodsk - Karelia - Russia - mailto:rnd at onego.ru -
 





More information about the Python-list mailing list