Programing

Python (List Comprehension) : 각 항목에 대해 두 개 이상의 항목 반환

crosscheck 2020. 10. 10. 09:36
반응형

Python (List Comprehension) : 각 항목에 대해 두 개 이상의 항목 반환


목록 이해의 각 항목에 대해 2 개 이상의 항목을 반환 할 수 있습니까?

내가 원하는 것 (예) :

[f(x), g(x) for x in range(n)]

돌아와야한다 [f(0), g(0), f(1), g(1), ..., f(n-1), g(n-1)]

따라서이 코드 블록을 대체 할 수 있습니다.

result = list()
for x in range(n):
    result.add(f(x))
    result.add(g(x))

>>> from itertools import chain
>>> f = lambda x: x + 2
>>> g = lambda x: x ** 2
>>> list(chain.from_iterable((f(x), g(x)) for x in range(3)))
[2, 0, 3, 1, 4, 4]

타이밍 :

from timeit import timeit

f = lambda x: x + 2
g = lambda x: x ** 2

def fg(x):
    yield f(x)
    yield g(x)

print timeit(stmt='list(chain.from_iterable((f(x), g(x)) for x in range(3)))',
             setup='gc.enable(); from itertools import chain; f = lambda x: x + 2; g = lambda x: x ** 2')

print timeit(stmt='list(chain.from_iterable(fg(x) for x in range(3)))',
             setup='gc.enable(); from itertools import chain; from __main__ import fg; f = lambda x: x + 2; g = lambda x: x ** 2')

print timeit(stmt='[func(x) for x in range(3) for func in (f, g)]',
             setup='gc.enable(); f = lambda x: x + 2; g = lambda x: x ** 2')


print timeit(stmt='list(chain.from_iterable((f(x), g(x)) for x in xrange(10**6)))',
             setup='gc.enable(); from itertools import chain; f = lambda x: x + 2; g = lambda x: x ** 2',
             number=20)

print timeit(stmt='list(chain.from_iterable(fg(x) for x in xrange(10**6)))',
             setup='gc.enable(); from itertools import chain; from __main__ import fg; f = lambda x: x + 2; g = lambda x: x ** 2',
             number=20)

print timeit(stmt='[func(x) for x in xrange(10**6) for func in (f, g)]',
             setup='gc.enable(); f = lambda x: x + 2; g = lambda x: x ** 2',
             number=20)

2.69210777094

3.13900787874

1.62461071932

25.5944058287

29.2623711793

25.7211849286


이중 목록 이해 :

[f(x) for x in range(5) for f in (f1,f2)]

데모:

>>> f1 = lambda x: x
>>> f2 = lambda x: 10*x

>>> [f(x) for x in range(5) for f in (f1,f2)]
[0, 0, 1, 10, 2, 20, 3, 30, 4, 40]

sum( ([f(x),g(x)] for x in range(n)), [] )

이것은 [f(1),g(1)] + [f(2),g(2)] + [f(3),g(3)] + ...

다음과 같이 생각할 수도 있습니다.

def flatten(list):
    ...

flatten( [f(x),g(x)] for x in ... )

note: The right way is to use itertools.chain.from_iterable or the double list comprehension. (It does not require recreating the list on every +, thus has O(N) performance rather than O(N^2) performance.) I'll still use sum(..., []) when I want a quick one-liner or I'm in a hurry, or when the number of terms being combined is bounded (e.g. <= 10). That is why I still mention it here, with this caveat. You can also use tuples: ((f(x),g(x)) for ...), () (or per khachik's comment, having a generator fg(x) which yields a two-tuple).


This lambda function zips two lists into a single one:

zipped = lambda L1, L2: [L[i] 
                         for i in range(min(len(L1), len(L2))) 
                         for L in (L1, L2)]

Example:

>>> f = [x for x in range(5)]
>>> g = [x*10 for x in range(5)]
>>> zipped(f, g)
[0, 0, 1, 10, 2, 20, 3, 30, 4, 40]

참고URL : https://stackoverflow.com/questions/11868964/python-list-comprehension-returning-two-or-more-items-for-each-item

반응형