comprehensions

Let’s appreciate how economical list comprehensions are. The following code stores words that contain the letter “o”, in a list:

words_with_o = []
word_collection = ['Python', 'Like', 'You', 'Mean', 'It']

for word in word_collection:
    if "o" in word.lower():
        words_with_o.append(word)

in a single line, using a list comprehension:

word_collection = ['Python', 'Like', 'You', 'Mean', 'It']
result = [w for w in word_collection if 'o' in w.lower()]

 

Nested list comprehensions

# This creates a 3x4 "matrix" (list of lists) of zeros.
>>> [[0 for col in range(4)] for row in range(3)]
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]

starmap()

map()은 iterable의 각 item을 다루고,

starmap()은 iterable안의 각 iterable을 다룬다.

starmap 다시말해 *map이란 말인데, iterable앞에 *를 달면 각 iterable안의 iterable을 가지고 함수에 적용하겠다는 말.

starmap() function

When an iterable is contained within another iterable and certain function has to be applied on them, starmap() is used. The starmap() considers each element of the iterable within another iterable as a separate item. It is similar to map(). This function comes under the category terminating iterators.

starmap()

map starmap

 

starmap(function, iterable)

starmap()

 

all() any()

all(), any() 둘다 True 또는 False를 리턴한다.

 

all()은 한개라도 False가 있으면 False를 리턴하고, 그렇지 않으면 True.

아무것도 없으면 True를 리턴한다.

 

any()는 한개라도 True가 있으면 True를 리턴하고, 그렇지 않으면 False.

 

allany