2

How do i find a max of a several tuples compared parallely which is stored in a dictionary.

d = {'one':(2,9,6), 'two':(7,2,1), 'three':(1,5,12)}

So, tuples (2,9,6),(7,2,1) and (1,5,12) 'zipped' produces the max i.e. (7,9,12).

Please help to advice.

(edited for the numbers confusion)

4

2 に答える 2

2

あなたの質問から、あなたが何を探しているのか完全にはわかりませんが、zip 部分は、これがあなたが望んでいるものだと思いました:

d = {'one':(1,2,3), 'two':(3,2,1), 'three':(4,5,6)}
tuple(max(x) for x in zip(*d.values()))

これは実際にタプルを一緒に圧縮し (最初のタプルの最初の要素と 2 番目のタプルの最初の要素など)、圧縮された各タプルの最大値を見つけます。

于 2012-06-01T04:53:24.233 に答える
2
d = {'one':(2,9,6), 'two':(7,2,1), 'three':(1,5,12)}
tuple(map(max, *d.values()))

$ python -m timeit -s"d = {'one':(2,9,6), 'two':(7,2,1), 'three':(1,5,12)}"\
                     "tuple(map(max, *d.values()))"
1000000 loops, best of 3: 1.08 usec per loop
$ python -m timeit -s"d = {'one':(2,9,6), 'two':(7,2,1), 'three':(1,5,12)}"\
                     "tuple(max(x) for x in zip(*d.values()))"
100000 loops, best of 3: 2.1 usec per loop
于 2012-06-01T05:22:51.437 に答える