Ask Your Question

python list comprehension 좀 누가 쉽게 설명해주세요.

asked 2017-02-10 07:38:12 -0600

pystock gravatar image

list comprehension 이 있는데 코드가 잘 이해가 되지 않습니다.

edit retag flag offensive close delete

2 Answers

Sort by » oldest newest most voted

answered 2018-10-04 14:00:51 -0600

List Comprehensions in Python List Comprehensions List comprehensions provide a concise way to create lists.

It consists of brackets containing an expression followed by a for clause, then zero or more for or if clauses. The expressions can be anything, meaning you can put in all kinds of objects in lists.

The result will be a new list resulting from evaluating the expression in the context of the for and if clauses which follow it.

The list comprehension always returns a result list. If you used to do it like this: new_list = [] for i in old_list: if filter(i): new_list.append(expressions(i)) You can obtain the same thing using list comprehension: new_list = [expression(i) for i in old_list if filter(i)]

Syntax The list comprehension starts with a '[' and ']', to help you remember that the result is going to be a list.

The basic syntax is [ expression for item in list if conditional ] This is equivalent to: for item in list: if conditional: expression

Let's break this down and see what it does

new_list = [expression(i) for i in old_list if filter(i)] new_list
The new list (result).

expression(i) Expression is based on the variable used for each element in the old list.

for i in old_list The word for followed by the variable name to use, followed by the word in the old list.

if filter(i) Apply a filter with an If-statement.

This blog shows an example of how to visually break down the list comprehension:

new_range = [i * i for i in range(5) if i % 2 == 0]

Which corresponds to:

result = [transform iteration filter ]

The * operator is used to repeat. The filter part answers the question if the item should be transformed. Examples Now when we know the syntax of list comprehensions, let's show some examples and how you can use it. Create a simple list Let's start easy by creating a simple list. x = [i for i in range(10)] print x

This will give the output:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] That's how you can create a simple list. Create a list using loops and list comprehension For the next example, assume we want to create a list of squares.

You can either use loops:

squares = []

for x in range(10): squares.append(x**2)

print squares [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

Or you can use list comprehensions to get the same result:

squares = [x**2 for x in range(10)]

print squares [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] Just remember the syntax: [ expression for item in list if conditional ] Multiplying parts of a list. Multiply every part of a list by three and assign it to a new list.

list1 = [3,4,5]

multiplied = [item*3 for item in list1]

print multiplied [9,12,15] Note how the item*3 multiplies each piece by 3. Show the first letter of each word We will take the first letter of each word and make a list out of it.

listOfWords = ["this","is","a","list","of","words"]

items = [ word[0] for word in listOfWords ]

print items The output should be: ['t', 'i', 'a', 'l', 'o', 'w'] Lower/Upper case converter Let's show how easy you can convert lower case / upper case letters.

[x.lower() for x in ["A","B","C"]] ['a', 'b', 'c']

[x.upper() for x in ["a","b","c"]] ['A', 'B', 'C'] Print numbers only from a given string This example show how to extract all the numbers from a string.

string = "Hello 12345 World" numbers = [x for x in string if x.isdigit()] print numbers

['1', '2', '3', '4', '5'] Change x.isdigit() to x.isalpha() if you don't want any numbers. Parsing a file using list comprehension In this example, we can see how to get specific lines out from a text file.

Create a text file and put in some text in it.

this is line1 this is line2 this is line3 this is line4 this is line5

Save the file as test.txt

Then create the filter by using list comprehension:

fh = open("test.txt", "r")

result = [i for i in fh if "line3" in i]

print result Output: ['this is line3 '] Using list comprehension in functions Now, let's see how we can use list comprehension in functions.

Create a function and name it double:

def double(x): return x*2

If you now just print that function with a value in it, it should look like this:

print double(10) 20 We can easily use list comprehension on that function.

[double(x) for x in range(10)]

print double [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

You can put in conditions:

[double(x) for x in range(10) if x%2==0] [0, 4, 8, 12, 16]

You can add more arguments:

[x+y for x in [10,30,50] for y in [20,40,60]] [30, 50, 70, 50, 70, 90, 70, 90, 110] See how you can put in conditions and add more arguments. Sources


edit flag offensive delete publish link more

answered 2017-02-10 07:43:04 -0600

pystock gravatar image

updated 2017-02-10 07:45:21 -0600

1~10 중 홀수로 구성된 리스트를 만들어 봅시다. 보통 다음과 같이 만들겠죠?

result = [ ]
for x in range(10):
    if x % 2 == 1:

list comprehension은 위와 같은 코드를 한 줄로 짧게 표현할 때 주로 사용합니다.

result = [ x for x in range(10) if x % 2 == 1]

위의 코드를 크게 세 부분으로 나눠서 살펴보시면 이해가 됩니다.

  • x # 리스트에 실제로 들어갈 놈을 바인딩하는 변수
  • for x in range(10) # 반복문
  • if x % 2 == 1 # 조건식이 있는 경우
edit flag offensive delete publish link more

Your answer

Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

Add answer

[hide preview]

Question tools

1 follower


Asked: 2017-02-10 07:38:12 -0600

Seen: 12,037 times

Last updated: Oct 04 '18