33

次のようなリストの辞書が与えられた場合

d = {'1':[11,12], '2':[21,21]}

どちらがよりPythonicであるか、そうでなければ好ましいですか:

for k in d:
    for x in d[k]:
        # whatever with k, x

また

for k, dk in d.iteritems():
    for x in dk:
        # whatever with k, x

または他に考慮すべきことがありますか?

編集、リストが役立つ場合(たとえば、標準の辞書は順序を保持しない)、これは適切かもしれませんが、はるかに遅くなります。

d2 = d.items()
for k in d2:
        for x in d2[1]:
            # whatever with k, x
4

4 に答える 4

8

私はいくつかの方法を検討しました:

import itertools

COLORED_THINGS = {'blue': ['sky', 'jeans', 'powerline insert mode'],
                  'yellow': ['sun', 'banana', 'phone book/monitor stand'],
                  'red': ['blood', 'tomato', 'test failure']}

def forloops():
    """ Nested for loops. """
    for color, things in COLORED_THINGS.items():
        for thing in things:
            pass

def iterator():
    """ Use itertools and list comprehension to construct iterator. """
    for color, thing in (
        itertools.chain.from_iterable(
            [itertools.product((k,), v) for k, v in COLORED_THINGS.items()])):
        pass

def iterator_gen():
    """ Use itertools and generator to construct iterator. """
    for color, thing in (
        itertools.chain.from_iterable(
            (itertools.product((k,), v) for k, v in COLORED_THINGS.items()))):
        pass

ipython とmemory_profilerを使用してパフォーマンスをテストしました。

>>> %timeit forloops()
1000000 loops, best of 3: 1.31 µs per loop

>>> %timeit iterator()
100000 loops, best of 3: 3.58 µs per loop

>>> %timeit iterator_gen()
100000 loops, best of 3: 3.91 µs per loop

>>> %memit -r 1000 forloops()
peak memory: 35.79 MiB, increment: 0.02 MiB

>>> %memit -r 1000 iterator()
peak memory: 35.79 MiB, increment: 0.00 MiB

>>> %memit -r 1000 iterator_gen()
peak memory: 35.79 MiB, increment: 0.00 MiB

ご覧のとおり、この方法はピーク時のメモリ使用量に目に見える影響はありませんでしたが、ネストされたforループは速度の点で無敵でした (読みやすさは言うまでもありません)。

于 2014-11-07T00:02:22.390 に答える
2

これがリスト内包表記のアプローチです。ネストされた...

r = [[i for i in d[x]] for x in d.keys()]
print r

[[11, 12], [21, 21]]
于 2013-08-17T14:26:31.570 に答える
2

Brionius コードからの私の結果:

         3 function calls in 0.173 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.173    0.173 <string>:1(<module>)
        1    0.173    0.173    0.173    0.173 speed.py:5(m1)
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Prof
iler' objects}


         4 function calls in 0.185 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.185    0.185 <string>:1(<module>)
        1    0.185    0.185    0.185    0.185 speed.py:10(m2)
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Prof
iler' objects}
        1    0.000    0.000    0.000    0.000 {method 'iteritems' of 'dict' obje
cts}
于 2013-08-17T15:04:42.007 に答える